Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7937935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:39:39+00:00 2026-06-03T22:39:39+00:00

I needed a function to return a string. I used the following code to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:39:40+00:00Added an answer on June 3, 2026 at 10:39 pm
    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
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I needed a function that simply checks if a string can be converted to
A (very long) while ago I regularly used the following code - then on
This php function retrieves a list of common words used in a string and
I needed to split a string by different delimiters. So i found & used
I was just working on a function that I needed to return two values,
Help needed. Im writing a function that returns result of ajax call but i
I have read about dynamically loading your class files when needed in a function
I needed to use in my old Git similar settings to the following to
writing a recursive string reverse function out of curiosity, but having a bit of
MSDN advises that RegisterWindowMessage() function is only used for registering messages to be sent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.