Confused by const

Discussion in 'C Programming' started by Mikhail Teterin, Dec 5, 2007.

  1. 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
    Mikhail Teterin, Dec 5, 2007
    #1
    1. Advertising

  2. Mikhail Teterin

    Guest

    On Dec 5, 3:52 pm, 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[..][..]?


    Change s to const char * const *
    , Dec 5, 2007
    #2
    1. Advertising

  3. Mikhail Teterin

    santosh Guest

    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.
    santosh, Dec 5, 2007
    #3
  4. 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, Dec 5, 2007
    #4
  5. 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
    Mikhail Teterin, Dec 5, 2007
    #5
  6. Mikhail Teterin <> 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.
    Ben Bacarisse, Dec 5, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,083
  2. Javier
    Replies:
    2
    Views:
    533
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,082
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    864
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    519
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page