| | |
warning when address of pointer to local variable is passed
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 10
Reputation:
Solved Threads: 0
Hi any one please tell me why the warning is generated when the address of pointer to a local variable is passed.
The following is the warning generated:
Icore.c:228: warning: passing argument 3 of foo from incompatible pointer type
The function declaration is:
int foo(void * , int , void**);
The following is the function definition:
int foo (void * inputbuffer, int sizeofinputbuffer, void** outputbuffer)
{
................
.............
}
The above function is invoked as:
unsigned char inputbuffer[] = {0x34,0x45,0x67,0x12,0x76};
int sizeofinputbuffer = 5;
unsigned char * outputbuffer = NULL;
int Total ;
Total = foo (inputbuffer, sizeofinputbuffer, &outputbuffer);
Thanks,
Prashanth
The following is the warning generated:
Icore.c:228: warning: passing argument 3 of foo from incompatible pointer type
The function declaration is:
int foo(void * , int , void**);
The following is the function definition:
int foo (void * inputbuffer, int sizeofinputbuffer, void** outputbuffer)
{
................
.............
}
The above function is invoked as:
unsigned char inputbuffer[] = {0x34,0x45,0x67,0x12,0x76};
int sizeofinputbuffer = 5;
unsigned char * outputbuffer = NULL;
int Total ;
Total = foo (inputbuffer, sizeofinputbuffer, &outputbuffer);
Thanks,
Prashanth
•
•
Join Date: Dec 2008
Posts: 10
Reputation:
Solved Threads: 0
Actually my intention is to pass the address of the pointer to char , ie to pass the address of the variable declared by char * outputbuffer. I want to pass the address of outputbuffer to foo, because I want foo to initialize the pointer with a valid address.
This would be possible only if I pass &outputbuffer to foo instead of outputbuffer itself(outputbuffer is initialized to NULL).
This would be possible only if I pass &outputbuffer to foo instead of outputbuffer itself(outputbuffer is initialized to NULL).
•
•
Join Date: Dec 2008
Posts: 10
Reputation:
Solved Threads: 0
void * is pointer to data of void type.
The size of void is 1(compiler dependent).
memcpy is implemented in some of the compilers such as that of INTEL such that it treats the arguments to be of type void *. Hence it just copies the data byte by byte from one location to the other as the size of void is 1.
void * means it points to any data of any type.
But you can not really dereferece a void * pointer just like that. You need to properly type cast the void pointer and then should be deferenced.
Similarly void** pneeds to be converted to suitable data type for ex int ** k = (int**) p;
And then you can do *k.
all these exercises you may do using char** pointer instead of using void **. It is better to use char ** instead of void **.
The size of void is 1(compiler dependent).
memcpy is implemented in some of the compilers such as that of INTEL such that it treats the arguments to be of type void *. Hence it just copies the data byte by byte from one location to the other as the size of void is 1.
void * means it points to any data of any type.
But you can not really dereferece a void * pointer just like that. You need to properly type cast the void pointer and then should be deferenced.
Similarly void** pneeds to be converted to suitable data type for ex int ** k = (int**) p;
And then you can do *k.
all these exercises you may do using char** pointer instead of using void **. It is better to use char ** instead of void **.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
Hi having void ** worked, I had to typecast the &outputbuffer to void **.
If your function assigns *outputbuffer to something to something that is not a char *, and then main() dereferences it, the result is undefined behaviour.
As you say at the end of the post, it is better to make the argument char **. Then, at least, the compiler can detect if you try to do a suspicious assignment in the function, or pass something incorrectly to it.
Last edited by grumpier; Jan 9th, 2009 at 8:20 am.
![]() |
Similar Threads
- Can Someone please check my project & comment/tips (C)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: Supply it
- Next Thread: The for loop programming style?
Views: 969 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax directory dynamic fflush file fork forloop framework frequency functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest homework inches incrementoperators kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault sequential shape socket socketprograming spoonfeeding stack standard string strings structures student systemcall threads turboc unix user variable voidmain() wab win32 windows.h






