Basically I have a struct with several members, named a-z. I have a string where I want each letter to correspond to the correct struct member. Currently I use a switch statement with each case in order to access the correct letter member of the struct, but Im wondering if theres a better (cleaner/shorter code) way to do this, without having to put 26 case statements with nearly identical code inside? My code looks somewhat like this:
typedef struct node
{
struct node *a;
struct node *b;
struct node *c;
...
struct node *z;
}node;
node *nTable[26][26][27];
int main
{
...
node *nWord = malloc(sizeof(node));
node *nPath = nWord;
nTable[0][0][0] = nWord;
char *cWord
cWord = "abcde";
for (int n = 0; n < 5; n++)
{
nWord = malloc(sizeof(node));
switch (cWord[n])
case 'a':
nPath->a = nWord;
nPath = nWord;
break;
case 'b':
nPath->b = nWord;
nPath = nWord;
...
case 'z':
nPath->z = nWord; //code is the same for each case, only difference between each is which member its assigned to
nPath = nWord;
}
}
Note the above code is a much reduced form of my actual code, so there may be some basic syntax and other errors within this example, but the general purpose of my code should be apparent.
So is there a cleaner way to do this, without using a case statement for each letter? Something simple like "nPath->cWord[n] = code;" would be perfect! (though this as it is, obviously does not work)
Any ideas would be great! Also sorry in advance if Ive left out any important info