![]() |
|
#1
|
|||
|
|||
Assigning an array of strings from a structureHi 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:
However, the original programmer has taken it upon himself in another file to separately do this: CPP / C++ / C Code:
Which is then used to translate a value into a day name with this: Code:
Like I said, changing that part is not really an option as other functions hang off the methodology and structures CPP / C++ / C Code:
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
|
|||
|
|||
Re: Assigning an array of strings from a structureQuote:
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:
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
|
|||
|
|||
Re: Assigning an array of strings from a structureThanks,
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 GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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