(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consi

Discussion in 'C Programming' started by lovecreatesbeauty, May 9, 2006.

  1. There is a warning/(error? I remember it is an error for line 10 on
    some compilers before. At least on g++, it is an error.) for line 10. I
    first read a similar example from `Expert C Programming -- Deep
    Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line
    10 is illegal?

    Is what Keith Thompson said in another post also helpful to understand
    this question:

    "... The implicit conversion rule applies only to void*, not to void**.
    In this context, a void* is just another object type, and a void** is
    just another pointer-to-object type. There's no implicit conversion
    from one pointer-to-object type to another pointer-to-object type. ..."

    void foo1(const char *p) {}
    void foo2(const char **p) {}

    int main(void)
    {
    char *p1;
    char **p2;

    foo1(p1); /* line 9: consistent type conversion */
    foo2(p2); /* line 10: inconsistent type conversion */
    }

    The gcc tells:
    $ gcc -W -Wall -std=c99 -pedantic test.c
    ....
    test.c: In function `main':
    test.c:10: warning: passing arg 1 of `foo2' from incompatible pointer
    type
     
    lovecreatesbeauty, May 9, 2006
    #1
    1. Advertising

  2. lovecreatesbeauty

    Ian Collins Guest

    Re: (const char *cp) and (char *p) are consistent type, (const char**cpp) and (char **pp) are not consistent

    lovecreatesbeauty wrote:
    > There is a warning/(error? I remember it is an error for line 10 on
    > some compilers before. At least on g++, it is an error.) for line 10. I
    > first read a similar example from `Expert C Programming -- Deep
    > Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line
    > 10 is illegal?
    >

    char pointer and const char pointer are both pointers to the same object
    type, char.

    A const char pointer and char pointer are different object types, so
    pointers to them are pointers to different object types.

    > Is what Keith Thompson said in another post also helpful to understand
    > this question:
    >
    > "... The implicit conversion rule applies only to void*, not to void**.
    > In this context, a void* is just another object type, and a void** is
    > just another pointer-to-object type. There's no implicit conversion
    > from one pointer-to-object type to another pointer-to-object type. ..."
    >

    Yes, Keith is pointing out that a pointer type is an object type and
    there isn't any implicit conversion between pointers to different types.

    > void foo1(const char *p) {}
    > void foo2(const char **p) {}
    >
    > int main(void)
    > {
    > char *p1;
    > char **p2;
    >
    > foo1(p1); /* line 9: consistent type conversion */
    > foo2(p2); /* line 10: inconsistent type conversion */
    > }
    >
    > The gcc tells:
    > $ gcc -W -Wall -std=c99 -pedantic test.c
    > ....
    > test.c: In function `main':
    > test.c:10: warning: passing arg 1 of `foo2' from incompatible pointer
    > type
    >

    Mine says
    "/tmp/x.c", line 10: warning: argument #1 is incompatible with prototype:
    prototype: pointer to pointer to const char : "/tmp/x.c", line 2
    argument : pointer to pointer to char

    Which a a bit clearer.
    --
    Ian Collins.
     
    Ian Collins, May 9, 2006
    #2
    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. DrUg13
    Replies:
    1
    Views:
    482
    DrUg13
    Feb 10, 2004
  2. Alex Vinokur
    Replies:
    7
    Views:
    414
    Greg Comeau
    Nov 15, 2004
  3. Vinu
    Replies:
    9
    Views:
    627
  4. Replies:
    24
    Views:
    841
    Netocrat
    Oct 30, 2005
  5. www.hitechskill.com
    Replies:
    0
    Views:
    1,361
    www.hitechskill.com
    Apr 9, 2006
Loading...

Share This Page