1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pointer and function ???

Discussion in 'C' started by frizi, Feb 1, 2007.

  1. frizi

    frizi New Member

    Joined:
    Jan 30, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Here is the code :
    Code:
    #include<stdio.h>
    
    int send() {
    
    	int p[2]={3,5};
    	 // lol = &p[0];
    	 return &p; 
    
    }
    
    int main() {
    	int *temp=send();
    	printf("value: %i | %i \n",*(temp),*(temp+1));
    	return(0);
    
    }
    /*
    Here is the reply from the compiler
    pp.c: In function ‘send’:
    pp.c:6: warning: return makes integer from pointer without a cast
    pp.c:6: warning: function returns address of local variable
    pp.c: In function ‘main’:
    pp.c:10: warning: initialization makes pointer from integer without a cast

    ... what's wrong ? It's just warning msg, It realy need to be changed something, I know that I dont encapsuled the function and with the result of function "send" can be manipulated but I dont know how else I can return a array. THanks for your help.
    */
     
    Last edited by a moderator: Feb 2, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    You are returning from the send function a pointer but the return type of send has some other views.
     
  3. frizi

    frizi New Member

    Joined:
    Jan 30, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    THnx but even without that int before send it's not workin good, what need to be changed ? Could you be more concrete please.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    There are more than one errors.

    1. The signature should be int* send()
    2. You should not be returning the address of local variable. Its not a valid pointer in the caller function.
     
  5. frizi

    frizi New Member

    Joined:
    Jan 30, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    // something liket that ? :
    #include<stdio.h>
    
    int* send() {
    	int p[2]={3,5};
    	 int *lol = &p[0];
    	 return lol; 
    }
    
    int main() {
    	int *temp=send();
    	printf("value: %i | %i \n",*(temp),*(temp+1));
    	return(0);
    }
    /* no error or warning message. It's bad if I use printf from send()+1. Should I use another function which shows at send()? It' working well jus't i wanna to be right. Thanks anyway :] */
     
    Last edited by a moderator: Feb 2, 2007
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    The pointer that you return from send is a local value that goes out of scope when send returns. It is available for use by other code declaring locals. It is even available for your processor to use for tracking the flow of the program. Your program works simply because you are referring to the old area before it is reused by something else. It is a distinct no-no. It is a crash in the making. Review the concept of local variables.
     

Share This Page