> Behold, a function which returns a pointer:
> int* test()
> {
> int i=5;
> return &i;
> }
> Why do I get a "Suspicious pointer conversion"-warning
> ? The functions using this concept seems to work OK.
> Should I make them differently.
The error message itself makes me suspicious about the quality of the
compiler. The correct warning (or preferably error) would be 'returning
address of the local variable' or something like that. The address itself
would be usable, but data that is stored at that location will most probably
be invalid sooner or later without any visible cause. Try this (should be
valid code):
void main(void)
{
int * ip;
ip = test(); // ip points to the location where 5
// is (was) stored
if (*ip == 5) // can be true at this point
printf("There is %d stored in the middle"
" of nowhere\n", *ip);
}
Most probably you won't get message saying that 5 is still there. printf()
uses pretty much space in stack, enough to recycle the storage test() points
to.
If yoy declare i as static int in test() it would make sense because i
will exist after test() returns address to it.
---
---------------
* Origin: A point in the middle of nowhere (2:490/31.3100)
|