Reference variable with string
hi all,
i have program contains many variables standard format:
int songnamenotes[]
&
int songnamebeats[]
eg: happybirthdaynotes & happybirthdaybeats
i able define variable used based on string contains song name.
so example, have:
and able address correct song's note , beat variables
what simplest way of doing this?
many thanks
i have program contains many variables standard format:
int songnamenotes[]
&
int songnamebeats[]
eg: happybirthdaynotes & happybirthdaybeats
i able define variable used based on string contains song name.
so example, have:
code: [select]
char songname[] = "happybirthday"and able address correct song's note , beat variables
what simplest way of doing this?
many thanks
so you'd write things like:
this not supported c. arrays can addressed integer indexes, not string indexes.
you example use #define associate song names "song indexes":
and on.
then can write things like:
(you must careful when assigning values arrays, of course).
if song name entered user, e.g. via serial port, have find way convert string integer. not quite elegant conceptually simple solution be:
hth
code: [select]
songnamenotes[songname]
songnamebeats[songname]
this not supported c. arrays can addressed integer indexes, not string indexes.
you example use #define associate song names "song indexes":
code: [select]
#define song_happybirthday 0
#define song_iloverocknroll 1
#define song_yoursong 2
and on.
then can write things like:
code: [select]
songnamenotes[song_happybirthday]
songnamebeats[song_happybirthday]
(you must careful when assigning values arrays, of course).
if song name entered user, e.g. via serial port, have find way convert string integer. not quite elegant conceptually simple solution be:
code: [select]
int songindex;
if (strcmp(songname, "happy birthday") == 0) {
songindex = song_happybirthday;
}
else if (strcmp(songname, "your song") == 0) {
songindex = song_yoursong;
}
// etc.
hth
Arduino Forum > Forum 2005-2010 (read only) > Software > Development > Reference variable with string
arduino
Comments
Post a Comment