Pointer and function ???

frizi's Avatar
Newbie Member
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 shabbir; 2Feb2007 at 10:02.. Reason: Code formating.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You are returning from the send function a pointer but the return type of send has some other views.
0
frizi's Avatar
Newbie Member
THnx but even without that int before send it's not workin good, what need to be changed ? Could you be more concrete please.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
frizi's Avatar
Newbie Member
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 shabbir; 2Feb2007 at 21:20.. Reason: Code formatting.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.