TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTIAN S. PEDERSEN
from: ANTHONY TIBBS
date: 1997-07-31 19:32:00
subject: Re: Conversion of pointe

 > 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 message you are receiving alone makes me suspicious as to the
 quality of the compiler you are using.  You SHOULD receive a message
 to the effect of "Returning address of local variable".
 
 You see, since "i" is declared locally to test(), when test() returns, 
 you have no guarantee that "5" will still be there.  If you used
 static int i=5, then you would be safe.
 
 Try this:
 int *test (void);
 
 int main (void)
 {
 int *value;
 
   value = test;
   printf ("The value of \"value\" is: %d\n",value);
   return 0;
 }
 int *test (void)
 {
   int *i = 5;
   return &i;
 }
 
 Since printf() uses quite a bit of stack space in its operations, you
 can be pretty sure that "value" will not be 5 when you check.
 
 
 
Sincerely,
Anthony Tibbs
... Ta-Sardar-Gor
___ Blue Wave/DOS v2.30 [NR]
--- Maximus 3.01
---------------
* Origin: World of Power BBS þ Private þ Ottawa, ON (1:163/215.38)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.