Can't dereference pointer to pointer? 
Author Message
 Can't dereference pointer to pointer?

Greetings,

int myfunc (char **c)
{
        if (!*c)
        {
                printf ("!*c\n");
        }

Quote:
}

int main (void)
{
        char c[16];
        myfunc (&c);

Quote:
}

test.c:12: warning: passing arg 1 of `myfunc' from incompatible pointer type

Why must I cast &c to (char **) to shut up the compiler?
Why does this print !*c when executed?

TiA,

sh



Mon, 20 Sep 2004 15:34:41 GMT  
 Can't dereference pointer to pointer?

Quote:
> Greetings,

> int myfunc (char **c)
> {
>         if (!*c)
>         {
>                 printf ("!*c\n");
>         }
> }

> int main (void)
> {
>         char c[16];
>         myfunc (&c);
> }

> test.c:12: warning: passing arg 1 of `myfunc' from incompatible pointer type

> Why must I cast &c to (char **) to shut up the compiler?

Because &c has type pointer to array of char, not pointer to pointer to char.
Read the FAQ:
http://www.eskimo.com/~scs/C-faq/top.html

Quote:
> Why does this print !*c when executed?

Note: Use a fixed-pitch font to view the diagram below.

char ** c   char c[16]
        |
        +-> [0][xxxx] <- check if this value is not null
            [1][xxxx]
            [2][xxxx]
            ...
            [16][xxxx]

if(!*c) checks whether the value that the pointer points to is not
null. This means that the first element of the array is interpreted as
a pointer and tested against the null-pointer constant, which is
undefined.

Bart.



Mon, 20 Sep 2004 16:07:11 GMT  
 Can't dereference pointer to pointer?

Quote:


> > Greetings,

> > int myfunc (char **c)
> > {
> >         if (!*c)
> >         {
> >                 printf ("!*c\n");
> >         }
> > }

> > int main (void)
> > {
> >         char c[16];
> >         myfunc (&c);
> > }

> > test.c:12: warning: passing arg 1 of `myfunc' from incompatible pointer type

> > Why must I cast &c to (char **) to shut up the compiler?

> Because &c has type pointer to array of char, not pointer to pointer to char.
> Read the FAQ:
> http://www.eskimo.com/~scs/C-faq/top.html

> > Why does this print !*c when executed?

> Note: Use a fixed-pitch font to view the diagram below.

> char ** c   char c[16]
>         |
>         +-> [0][xxxx] <- check if this value is not null
>             [1][xxxx]
>             [2][xxxx]
>             ...
>             [16][xxxx]

That last index should be 15, of course.

Bart.



Mon, 20 Sep 2004 16:10:02 GMT  
 Can't dereference pointer to pointer?

Quote:

> Because &c has type pointer to array of char, not pointer to pointer to
char.
> Read the FAQ:
> http://www.eskimo.com/~scs/C-faq/top.html

Hm... it seems I've fallen victim to the assumption that arrays are just
initialised pointers. *reads*

sh



Mon, 20 Sep 2004 16:13:46 GMT  
 Can't dereference pointer to pointer?


Quote:
>Greetings,

>int myfunc (char **c)
>{
>        if (!*c)
>        {
>                printf ("!*c\n");
>        }
>}

>int main (void)
>{
>        char c[16];
>        myfunc (&c);
>}

>test.c:12: warning: passing arg 1 of `myfunc' from incompatible pointer type

Right. &c evaluates to a pointer to an array of 16 char. myfunc()
expects a pointer to pointer to char; they're different types.

In order to get around this (and retain the types of c and myfunc),
you can assign c to a pointer and then pass the address of the
pointer:

char c[16];
char *ptr=c;
myfunc(&ptr);

Russ



Mon, 20 Sep 2004 16:17:32 GMT  
 Can't dereference pointer to pointer?

Quote:


> > Because &c has type pointer to array of char, not pointer to pointer to
> char.
> > Read the FAQ:
> > http://www.eskimo.com/~scs/C-faq/top.html

> Hm... it seems I've fallen victim to the assumption that arrays are just
> initialised pointers. *reads*

Most of the time, the name of an array converts to a pointer to the
first element. There are a few exceptions, the sizeof operator being
one, and the one that got you, the unary & operator.

It's a somewhat tricky point, that gets a lot of people early on.

Brian Rodenborn



Tue, 21 Sep 2004 01:28:18 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. odd pointer dereference problem

2. dereference a pointer to a variable

3. dereference of a zero pointer

4. Dereference void pointer help, xxgdb help please

5. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

6. Pointer to Pointer won't work...

7. memory leak: pointer->pointer->pointer->struct

8. Pointer of Pointers was Pointer of arrays...

9. memory block containing pointers, aka pointer to pointer i believe

10. pointers pointers pointers!!!

11. memory Leak: pointer->pointer->pointer->struct

12. Pointer Functions and Pointers to Pointer Functions

 

 
Powered by phpBB® Forum Software