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 7578993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:33:55+00:00 2026-05-30T17:33:55+00:00

typedef struct { int A; int B; char* C; // problem is here }foo;

  • 0
typedef struct
{   
    int A;
    int B;

    char* C; // problem is here
}foo;

int SetA(void)
{
    ....
    return retval;
}

int SetB(void)
{
    ....
    return retval;
}

const char* bar(void)
{
    .....
    char* retval="return val";
    .....
    return retval;
}

void SetFoo(foo* paramFoo)
{
paramFoo->A = SetA();
paramFoo->B = SetB();

paramFoo->C = bar();

}


static foo staticFoo;

void main()
{   
    SetFoo(&staticFoo);
    printf("%s",staticFoo.C);// printing
}

everything will go fine but the “char*C” in struct foo will not be written well. why? i need to know if i did mistake and how too correct it?. i’ve ran the bar() and it returns the correct value.

Thank you
~ Max

  • 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-05-30T17:33:56+00:00Added an answer on May 30, 2026 at 5:33 pm

    I would prefer using malloc and later free it, so that I dont have to worry about when the read only string literal get destroyed.

     const char* bar(void) 
     {     
         const char* retval="return val";
         char * value = malloc( strlen(retval) + 1 ); // +1 for the termination 
                                                      // character
         strcpy( value, retval );
         retrun value ;
     }
    

    Remember to free the return value when no longer needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; void
typedef struct child {int count; char word[100]; inner_list*next;} child; typedef struct parent { char
I currently have a struct typedef struct Entry { int counter; void *block; }
Coding in C: typedef struct { char *string; int one; int two; } Example;
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
Consider the following: typedef struct { int a; int b; int c; int d;
I am curious why this code works: typedef struct test_struct { int id; }
Let's say I have a first structure like this: typedef struct { int ivalue;
Given a struct, e.g. typedef struct { int value; } TestStruct; Why does the

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.