Confused by const

  • Thread starter Mikhail Teterin
  • Start date
M

Mikhail Teterin

What's wrong with this program?

#include <stdio.h>
#include <stdlib.h>

static void
meow(const char * const *p)
{
printf("%p\n", p);
}

int
main()
{
char * const *s;

s = malloc(20);

meow(s);

free(s);
}

Why is the call to meow() generate a warning:
t.c:16: warning: passing arg 1 of `meow' from incompatible pointer type

How can the program be corrected without reducing the promise, that meow()
will not modify p[..] AND p[..][..]?

Thanks!

-mi
 
V

vippstar

What's wrong with this program?

#include <stdio.h>
#include <stdlib.h>

static void
meow(const char * const *p)
{
printf("%p\n", p);
}

int
main()
{
char * const *s;

s = malloc(20);

meow(s);

free(s);
}

Why is the call to meow() generate a warning:
t.c:16: warning: passing arg 1 of `meow' from incompatible pointer type

How can the program be corrected without reducing the promise, that meow()
will not modify p[..] AND p[..][..]?

Change s to const char * const *
 
S

santosh

Mikhail said:
What's wrong with this program?

#include <stdio.h>
#include <stdlib.h>

static void
meow(const char * const *p)
{
printf("%p\n", p);
}

int
main()
{
char * const *s;

s = malloc(20);

meow(s);

free(s);
}

Why is the call to meow() generate a warning:
t.c:16: warning: passing arg 1 of `meow' from incompatible
pointer type

How can the program be corrected without reducing the promise, that
meow() will not modify p[..] AND p[..][..]?

Thanks!

Prototype meow() as:

static void meow(const char *p);

Note also that the 'p' format specifier in printf() expects a void
pointer. So you need to cast 'p' appropriately.
 
M

Mikhail Teterin

Change s to const char * const *

But than the main function must be able to alter the contents of whatever s
points to. If I follow your advice, I can't even free that memory:

warning: passing arg 1 of `free' discards qualifiers from pointer target
type
 
M

Mikhail Teterin

santosh said:
Prototype meow() as:

static void meow(const char *p);

Oh, come on.

What about a meow(), which actually needs to treat the argument as a pointer
to pointer:

static void
meow(const char * const *s)
{
printf("%p\n", (void *)s[0]);
}

?

-mi
 
B

Ben Bacarisse

Mikhail Teterin said:
What's wrong with this program?

#include <stdio.h>
#include <stdlib.h>

static void
meow(const char * const *p)
{
printf("%p\n", p);
}

int
main()
{
char * const *s;

s = malloc(20);

meow(s);

free(s);
}

Why is the call to meow() generate a warning:
t.c:16: warning: passing arg 1 of `meow' from incompatible
pointer type

Simply because that is what C says. The rule for pointer
compatibility was made simple but restrictive. There have been a few
discussions of this in the last year or so. Search for them if you'd
like to know more history.
How can the program be corrected without reducing the promise, that meow()
will not modify p[..] AND p[..][..]?

You have these choices:

(1) Live with the warning.
(2) Cast the pointer when you call.
(3) Switch to C++ :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top