Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Confused by const (http://www.velocityreviews.com/forums/t557535-confused-by-const.html)

Mikhail Teterin 12-05-2007 01:52 PM

Confused by const
 
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

vippstar@gmail.com 12-05-2007 01:57 PM

Re: Confused by const
 
On Dec 5, 3:52 pm, Mikhail Teterin <usenet+m...@aldan.algebra.com>
wrote:
> 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 *

santosh 12-05-2007 02:00 PM

Re: Confused by const
 
Mikhail Teterin wrote:

> 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.


Mikhail Teterin 12-05-2007 02:05 PM

Re: Confused by const
 
vippstar@gmail.com wrote:

> 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



Mikhail Teterin 12-05-2007 02:14 PM

Re: Confused by const
 
santosh wrote:

> 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




Ben Bacarisse 12-05-2007 04:23 PM

Re: Confused by const
 
Mikhail Teterin <usenet+meow@aldan.algebra.com> writes:

> 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++ :-)

--
Ben.


All times are GMT. The time now is 09:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.