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: prashanth s j is an unknown quantity at this point 
Solved Threads: 0
prashanth s j prashanth s j is offline Offline
Newbie Poster

warning when address of pointer to local variable is passed

 
0
  #1
Dec 29th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: warning when address of pointer to local variable is passed

 
0
  #2
Dec 29th, 2008
Easy, make outputbuffer a void*
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 10
Reputation: prashanth s j is an unknown quantity at this point 
Solved Threads: 0
prashanth s j prashanth s j is offline Offline
Newbie Poster

Re: warning when address of pointer to local variable is passed

 
0
  #3
Dec 29th, 2008
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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: warning when address of pointer to local variable is passed

 
0
  #4
Dec 29th, 2008
Either

int foo (void * inputbuffer, int sizeofinputbuffer, unsigned char** outputbuffer)

or

int foo (void * inputbuffer, int sizeofinputbuffer, void* outputbuffer)
and perform the appropriate cast inside the function to get to a proper unsigned char**
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 10
Reputation: prashanth s j is an unknown quantity at this point 
Solved Threads: 0
prashanth s j prashanth s j is offline Offline
Newbie Poster

Re: warning when address of pointer to local variable is passed

 
0
  #5
Dec 30th, 2008
Hi having void ** worked, I had to typecast the &outputbuffer to void **.
Thanks,
Prashanth
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: somnathsarode is an unknown quantity at this point 
Solved Threads: 1
somnathsarode's Avatar
somnathsarode somnathsarode is offline Offline
Newbie Poster

Re: warning when address of pointer to local variable is passed

 
0
  #6
Jan 1st, 2009
hi please tell meaning of **void and ***void pointers
also help me where i got info about this
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 10
Reputation: prashanth s j is an unknown quantity at this point 
Solved Threads: 0
prashanth s j prashanth s j is offline Offline
Newbie Poster

Re: warning when address of pointer to local variable is passed

 
0
  #7
Jan 9th, 2009
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 **.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: warning when address of pointer to local variable is passed

 
0
  #8
Jan 9th, 2009
Originally Posted by prashanth s j View Post
Hi having void ** worked, I had to typecast the &outputbuffer to void **.
Maybe so, but that doesn't make it a good idea.

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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 969 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC