pointer-to-pointer usage 
Author Message
 pointer-to-pointer usage

Greetings,

        Could someone explain the following warnings that I get when
I compile with GNU compiler?  Please ignore the -D switches in the
compiler statement; I am developing for vxWorks, but the OS should not
make a difference.  Obviously, the functions do not do anything useful,
but are just an extraction of my actual code.

cc68k -D__VX__ -I/usr/vw/h -I/export/home/cheng/spc/h -Wall -ansi -DCPU=MC68040 -DCPU_SPEED=25MHz  -nostdinc -g -O2 -c test.c
test.c: In function `call_change':
test.c:34: warning: passing arg 1 of `change_ptr' from incompatible pointer type
test.c:35: warning: passing arg 1 of `change_ptr' from incompatible pointer type
test.c:36: warning: passing arg 1 of `change_ptr' from incompatible pointer type
test.o completed

In particular, the first four function calls to change_ptr() are
equivalent, but only the last one does not generate a warning.  Also,
the declaration and usage of change_ptr_2 does not generate any warnings.

So my question is why should I have to put in an extra cast, and/or
declare a pointer type to my structure just so that the compiler
will not generate these warnings?

Thanks in advance
        John Cheng

test.c:
#include <stdlib.h>

typedef struct {
        int     a;
        char    b;
        } tempType, *tempPtr;

/* test functions */
int change_ptr(tempType **temp)
{
free(*temp);
*temp = NULL;
return 0;

Quote:
}

int change_ptr_2(tempPtr *temp)
{
free(temp);
temp = NULL;
return 0;

Quote:
}

id call_change (void)
{
tempType        *temp1;
tempPtr         temp2;

temp1 = malloc (sizeof(tempType));

(*temp1).a = 1;
(*temp1).b = 'a';

change_ptr(temp1);
change_ptr(&(*temp1));
change_ptr(&*temp1);
change_ptr((tempType **)temp1);
change_ptr_2(&temp2);

Quote:
}

/* end of test.c */


Sat, 05 Sep 1998 03:00:00 GMT  
 pointer-to-pointer usage
The function "int change_ptr( tempType **temp )" expects to see a pointer
to a pointer to a tempType.  In the lines which are generating errors, you
are only passing a pointer to tempType.

Here is what each does:

Quote:
>change_ptr(temp1);

This is simply a copy of temp1, which is a pointer to tempType.  You would
need to take its address, for example:

change_ptr( &temp1 );

Quote:
>change_ptr(&(*temp1));
>change_ptr(&*temp1);

In both of the above, you are using * to resolve temp1 to the actual
tempType before taking the address.  

Quote:

>Greetings,
>    Could someone explain the following warnings that I get when
>I compile with GNU compiler?  Please ignore the -D switches in the
>compiler statement; I am developing for vxWorks, but the OS should not
>make a difference.  Obviously, the functions do not do anything useful,
>but are just an extraction of my actual code.

[...]

Quote:
>In particular, the first four function calls to change_ptr() are
>equivalent, but only the last one does not generate a warning.  Also,
>the declaration and usage of change_ptr_2 does not generate any warnings.

[...]

Quote:
>int change_ptr(tempType **temp)

[...]

Quote:
>tempType        *temp1;
>tempPtr         temp2;

>change_ptr(temp1);
>change_ptr(&(*temp1));
>change_ptr(&*temp1);
>change_ptr((tempType **)temp1);
>change_ptr_2(&temp2);

---

phone/fax (847) 439-7714


Sun, 06 Sep 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

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

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

3. Pointer of Pointers was Pointer of arrays...

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

5. pointers pointers pointers!!!

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

7. Pointer Functions and Pointers to Pointer Functions

8. Pointer to Pointer to Pointer....

9. Question on pointer-to-pointer-to-pointer

10. How to use (pointer to function), and function and pointer to (pointer to function)

11. double pointer (or pointer to pointer)

12. Pointers: return of pointer to array of pointers to main

 

 
Powered by phpBB® Forum Software