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

  • Thread starter lovecreatesbeauty
  • Start date
L

lovecreatesbeauty

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
 
I

Ian Collins

lovecreatesbeauty said:
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.
 

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

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top