0

I have structure

typedef struct 
{
    char employee_name[32];
    char organisation[32];

}info;

How can I Initialized a single or more elements of info .

I am doing like this at the start of the code:

info info_data= {
    {'d','a','v','i','d',' ','s','c','h','o','l','e','s','\0'},
    {'x','y','z',' ','I','n','c','\0'} 

};

This works fine but I want to avoid putting all the names with each character enclosed in ' ' and adding '\0' at the end.Is there a better way to implement this.Code has to run in an embedded processor and needs to be memory and speed optimized.

Raulp
  • 7,758
  • 20
  • 93
  • 155
  • Regarding to your request for memory and speed optimization: Do you want to change the content of `info` at runtime? If not, you could define `info`as const. This saves RAM but costs flash. And const would need no initialization at runtime. – Habi Oct 28 '15 at 07:13
  • I need it to be dynamically assigned. – Raulp Oct 28 '15 at 07:30

2 Answers2

2

Try this:

info info_data= {
    {"David Scholes"},
    {"xyz Inc"} 
};

You can initialise a character array with a string.

If you want to use strcpy then proceed as follows:

strcpy(info_data.employee_name, "David Scholes");
strcpy(info_data.organisation, "xyz Inc");
haccks
  • 104,019
  • 25
  • 176
  • 264
1

How about

typedef struct 
 {
         char employee_name[32];
         char organisation[32];

 }info;


 info info_data = {{"david scholes"}, {"xyz Inc"}};
Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42