Reputation: 8065
I needed a function to return a string. I used the following code to declare the function:
const char* serv_con(char app_data[50])
{
char send_data[1024],recv_data[1024];
//i am avoiding code segments irrelevant to the issue.
return recv_data;
}
and then called the function in the main like this:
int main()
{
char ser_data[50], app_data[50];
ser_data[0] = '\0';
app_data[0] = '\0';
//avoiding code segments irrelevant to the issue.
app_data = serv_con(ser_data); //function call
}
On compiling it gives the error:
connect.c:109: error: incompatible types when assigning to type ‘char[50]’ from type ‘const char *’
Then i replaced the const char in the declaration with std::string. The declaration now is as follows:
std::string serv_con(char app_data[50])
{
char send_data[1024],recv_data[1024];
//avoiding code segments irrelevant to the issue.
return recv_data;
}
And called it in the same way as mentioned above. But still it gives the following error on compiling:
connect.c:13: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
Please tell me how I can give a string as a return type from a function. The platform I work on is linux. Thanks in advance.
Upvotes: 2
Views: 24301
Reputation: 1
From your question I can be pretty sure that you are a beginner. Please go through a good text book i.e The C Programming Language.
Along with the error you must be getting this.
warning: function returns address of local variable.
Look at the warnings always. The question is already answered by Greg.
Upvotes: 0
Reputation: 5535
Returning pointer to local array or the array itself is a bad idea.
Even if you do malloc inside the function, the caller needs to free that memory too which is again not a very good practice.
I suggest the best way is to pass an out parameter i.e. pass the array in which you want the result to be copied as a parameter.
int serv_con(char app_data[50], char recv_data[50])
{
char send_data[1024];
//recv_data[1024];
//i am avoiding code segments irrelevant to the issue.
return 1; // return status success or failure which can be tested in main
}
and then called the function in the main like this:
int main()
{
char ser_data[50], app_data[50];
ser_data[0] = '\0';
app_data[0] = '\0';
//avoiding code segments irrelevant to the issue.
serv_con(ser_data, app_data); //function call
}
I found and corrected a slight mistake. The app_data you are passing in main is of size 50 where as the one you were trying to return from the function serv_con was 1024. This should be consistent. I have used both of size 50 in code above.
Upvotes: 1
Reputation: 7468
const char* serv_con(char app_data[50])
{
char send_data[1024],recv_data[1024];
//i am avoiding code segments irrelevant to the issue.
return recv_data;
}
this cannot work, since you're returning a pointer to a local variable, which is invalid after returning. You need to allocate recv_data
on the heap in order to use it after returning
char* serv_con(char app_data[50])
{
char send_data[1024];
char *recv_data = malloc(1024);
if (!recv_data)
return NULL;
// ...
return recv_data;
}
then change the main function to be something like
int main()
{
char ser_data[50];
char *app_data;
ser_data[0] = '\0';
//avoiding code segments irrelevant to the issue.
app_data = serv_con(ser_data); //function call
if (!app_data) {
// error
}
}
Upvotes: 9
Reputation: 4671
What you are doing is a really bad idea. The serv_con
function allocates some space on the stack for the recv_data
array and then returns a pointer to that location. Of course, as soon as you call another function that data will be wiped out, leading to a bug that is difficult to diagnose. If you must return a block of memory from a function, allocate it using malloc
. Basically, never return pointers to objects on the stack.
When you get the pointer out of serv_con
you assign it to app_data
, which already has some space allocated. There is no reason for doing this: simply declare app_data
as a char *
that will point to the storage allocated in serv_con
.
Upvotes: 3