user1842845
user1842845

Reputation: 31

using int counter in array name

In C I'm wondering if I can use an int as a counter to select an array from my list of arrays. If not how would you suggest getting around this? Sorry the code might explain it better:

edit: Sorry i should have clarified by saying that inputchar is a single character, im trying to see if inputchar is a letter in any of the words in the arrays

int i;

char word0[] ={'c','a','c','h','e'};
char word1[] ={'i','n','t','e','l'};
char word2[] ={'e','t','h','e','r','n','e','t'};
char word3[] ={'g','i','g','a','b','y','t','e'};
char word4[] ={'l','i','n','u','x'};

for (input = 0;input<i;input++)
{
    if (inputchar==word(i)[input] /*this is the problem here, i want to use the int (i) to chose the array, i is randomized before this in the program */
    {
        w(i)[input]=inputchar;
        lc++;
        printf("You guessed correct! continue on word master");
    }
}

Upvotes: 2

Views: 204

Answers (5)

Paul R
Paul R

Reputation: 213059

The trick is to change:

char word0[] ={'c','a','c','h','e'};
char word1[] ={'i','n','t','e','l'};
char word2[] ={'e','t','h','e','r','n','e','t'};
char word3[] ={'g','i','g','a','b','y','t','e'};
char word4[] ={'l','i','n','u','x'};

to e.g.:

char word[5][10] = { "cache",
                     "intel",
                     "ethernet",
                     "gigabyte",
                     "linux" };

and then you can write e.g.

    if (inputchar == word[i][input])
        ...

Upvotes: 1

John Bode
John Bode

Reputation: 123548

The simplest workaround is an array of arrays:

char word[][9] = {"cache", "intel", "ethernet", "gigabyte", "linux"};

This declares word as a 5-element array of 9-element arrays of char; the 5 is determined from the number of items in the initializer, and the 9 is required to hold the longest strings (8 characters plus the 0 terminator).

This does mean that there's some unused space in the first, second, and fifth string, but for an exercise like this that's probably not a big deal. If you were dealing with several thousand strings, it would be better to declare an array of pointers, then allocate each string separately so that you're only using as much memory as each string needs:

char *word[N]; // for N strings;
...
word[i] = malloc(length_of_string_for_this_element + 1);
if (word[i])
  strcpy(word[i], string_for_this_element);

When you're done, you would need to free that memory for each word:

for (i = 0; i < N; i++)
  free(word[i]);

Either way, you would access a specific character as

word[i][input]

Upvotes: 1

Alberto Bonsanto
Alberto Bonsanto

Reputation: 18042

Maybe you can use this (an array of pointers), If you don't need to modify the items.

const char *words[ 5 ] = { "cache", "intel", "ethernet", "gigabyte", "linux" };

This is how it works:

How it works enter image description here

This is an example from C, How to Program by Deitel & Deitel.

Upvotes: 0

Steve Jessop
Steve Jessop

Reputation: 279315

The simplest change to what you have is to create an array of pointers to each of your existing arrays:

char *words[] = { word0, word1, word2, word3, word4 };

Because your arrays aren't nul-terminated, you will also need to know their lengths, and you can't access that from words:

size_t wordlengths[] = { sizeof(word0), sizeof(word1), sizeof(word2), sizeof(word3), sizeof(word4) };

But, if your code doesn't actually rely on having 5 different writeable arrays of different sizes, containing non-terminated character data, then you might be able to do better.

Upvotes: 3

chrisaycock
chrisaycock

Reputation: 37928

Why not use an array of strings (pointers)?

char* words[5];

Then assign the individual strings:

words[0] = "cache";
words[1] = "intel";
words[2] = "ethernet";
words[3] = "gigabyte";
words[4] = "linux";

Now you can access it with an array index:

words[i][input]

Upvotes: 1

Related Questions