GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-May-2009, 14:46
FridgeFreezer FridgeFreezer is offline
New Member
 
Join Date: May 2009
Posts: 2
FridgeFreezer is on a distinguished road
Question

Assigning an array of strings from a structure


Hi guys, I'm hoping you can help me here since this site pops up in all my google searches trying to solve this problem.

Long story short, I'm working on someone else's old code on an embedded system that the customer wants updated to add more languages. I've done various programming over the years but am relatively new to C and I'm convinced I'm just missing some simple method here.

Unfortunately we're somewhat bound by the way the last guy put it together, so altering the rest of the code to suit is not really an option as there's quite a lot of it and other things are likely to break as a consequence.

Anyway, to business:

We have a language-specific structure populated at startup along these lines:

CPP / C++ / C Code:
void load_english(struct conf_struct *conf)
{			
	system_data.foo.msg[MSG_MONDAY]= "Monday";
        system_data.foo.msg[MSG_TUESDAY]= "Tuesday";
        system_data.foo.msg[MSG_WEDNESDAY] = "Wednesday";
        system_data.foo.msg[MSG_THURSDAY] = "Thursday";
        system_data.foo.msg[MSG_FRIDAY] = "Friday";
        system_data.foo.msg[MSG_SATURDAY]= "Saturday";
        system_data.foo.msg[MSG_SUNDAY] = "Sunday";
        [i]....continued for all other system messages[/i]

However, the original programmer has taken it upon himself in another file to separately do this:

CPP / C++ / C Code:
const char *day_list[] = 
{
		"Monday",
		"Tuesday",
		"Wednesday",
		"Thursday",
		"Friday",
		"Saturday",
		"Sunday",
		""
};

Which is then used to translate a value into a day name with this:
Code:
strcpy(msg, day_list[datetime_str2day(ctrl->mptr->setting)]);

Like I said, changing that part is not really an option as other functions hang off the methodology and structures so what I was hoping to do was populate the array thus:

CPP / C++ / C Code:
const char *day_list[] = 
{
	system_data.foo.msg[MSG_MONDAY]= "Monday",
        system_data.foo.msg[MSG_TUESDAY]= "Tuesday",
        system_data.foo.msg[MSG_WEDNESDAY] = "Wednesday",
        system_data.foo.msg[MSG_THURSDAY] = "Thursday",
        system_data.foo.msg[MSG_FRIDAY] = "Friday",
        system_data.foo.msg[MSG_SATURDAY]= "Saturday",
        system_data.foo.msg[MSG_SUNDAY] = "Sunday",
			""
};

Which (correctly) throws up a compiler error due to trying to assign something to a const. If you remove the const, it still fails though. I have tried various dereferencing / address pointer style iterations of this but can't persuade any of them to actually compile. I'm sure there must be some simple way of making the elements of day_list[] point to MSG_<day> but have totally hit the wall of deciphering the call(s) required to make it behave.

Can anyone help?
Last edited by LuciWiz : 11-May-2009 at 14:57. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 11-May-2009, 17:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Assigning an array of strings from a structure


Quote:
Originally Posted by FridgeFreezer
...making the elements of day_list[] point to MSG_<day>...
You can do it in two steps:


1. Declare the day_list array.

2. Call a function that sets the values of the array. (You could just write a loop or write the eight assignment statements in-line, but I think a function makes the main program a little cleaner, even if it is only used once.)

CPP / C++ / C Code:
/* 
 * An enum data type is a set of aliases for named integers
 *
 * The first will have a value of zero, and the others follow sequentially
 */
enum weekdays {
    MSG_MONDAY,
    MSG_TUESDAY,
    MSG_WEDNESDAY,
    MSG_THURSDAY,
    MSG_FRIDAY,
    MSG_SATURDAY,
    MSG_SUNDAY,
    MSG_INVALID
};

/* 
 * Or you could have #defined values as macros or
 * you could just declare them as global constants
 */



/* Function to set day_list array values from a conf_struct */
void set_day_list(const char *days[], const struct conf_struct *conf)
{
    int i;
    for (i = MSG_MONDAY; i <= MSG_INVALID; i++) {
        days[i] = conf->foo.msg[i];
    }
}

.
.
.
int main()
{
    /* declare a conf_struct */
    struct conf_struct eng_conf;

    /* declare the day_list */
    const char *day_list[8];

    /* Give values to eng_conf unless you had an initializer list */
.
.
.
    /* Call the function to set the day_list members from the conf_struct */

    set_day_list(day_list, &eng_conf);
.
.
.

Regards,

Dave

Footnote: As I tried to indicate in the comments, enumerated data types don't really buy you much in C. They are just integers with names, and there is no way that a program can connect a named integer with some C-style "string." The programmer has to keep track of such things. In C++, declaring an enumerated data type is actually like a typedef, and type checking in function calls and other expressions can offer some protection against their misuse, but in C they are just ints. Even in C++, there is no way that a program connects an enum value with a "string" other than under direct explicit control of the programmer.

I think many C programmers would just use global constants, and some might even use #defined macros.
Last edited by davekw7x : 11-May-2009 at 18:45. Reason: Typographical errors
  #3  
Old 12-May-2009, 12:38
FridgeFreezer FridgeFreezer is offline
New Member
 
Join Date: May 2009
Posts: 2
FridgeFreezer is on a distinguished road

Re: Assigning an array of strings from a structure


Thanks,
I didn't do it exactly as you described but your post was very helpful in working out what needed to be done - it now all works fine.

I ended up making it an external variable and then assigning it the "dumb" way (day_list[n] = MSG_<day>) as this seemed to work best with the code we have.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 16:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
Returning a 2 dimensional Array from a function vicky_brsh C++ Forum 1 04-Jan-2008 15:06
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.