C 在数组名中使用int计数器

C 在数组名中使用int计数器,c,arrays,int,C,Arrays,Int,在C语言中,我想知道是否可以使用int作为计数器从数组列表中选择数组。如果不是的话,你建议如何解决这个问题?抱歉,代码可能会更好地解释它: 编辑:对不起,我应该澄清说inputchar是一个字符,我想看看inputchar是否是数组中任何单词的字母 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',

在C语言中,我想知道是否可以使用
int
作为计数器从数组列表中选择数组。如果不是的话,你建议如何解决这个问题?抱歉,代码可能会更好地解释它:

编辑:对不起,我应该澄清说inputchar是一个字符,我想看看inputchar是否是数组中任何单词的字母

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");
    }
}
inti;
char-word0[]={'c','a','c','h','e'};
char-word1[]={'i','n','t','e','l'};
字符2[]={'e','t','h','e','r','n','e','t'};
字符3[]={'g'、'i'、'g'、'a'、'b'、'y'、't'、'e'};
char-word4[]={'l','i','n','u','x'};

对于(input=0;input为什么不使用字符串数组(指针)

然后分配各个字符串:

words[0] = "cache";
words[1] = "intel";
words[2] = "ethernet";
words[3] = "gigabyte";
words[4] = "linux";
现在,您可以使用数组索引访问它:

words[i][input]

诀窍在于改变:

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'};
例如:

char word[5][10] = { "cache",
                     "intel",
                     "ethernet",
                     "gigabyte",
                     "linux" };
然后你就可以写

    if (inputchar == word[i][input])
        ...
如果不需要修改项目,也许可以使用这个(指针数组)

const char *words[ 5 ] = { "cache", "intel", "ethernet", "gigabyte", "linux" };
这就是它的工作原理:


这是C中的一个示例,Deitel&Deitel如何编程。

对现有数组的最简单更改是创建指向每个现有数组的指针数组:

char *words[] = { word0, word1, word2, word3, word4 };
因为您的数组不是以nul结尾的,所以您还需要知道它们的长度,并且您不能从
中访问它们:

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

但是,如果您的代码实际上不依赖于具有5个不同大小、包含非终止字符数据的不同可写数组,那么您可能可以做得更好。

最简单的解决方法是数组数组:

char word[][9] = {"cache", "intel", "ethernet", "gigabyte", "linux"};
这将
word
声明为
char
的9元素数组中的5元素数组;5是根据初始值设定项中的项数确定的,9需要包含最长的字符串(8个字符加上0终止符)

这确实意味着在第一个、第二个和第五个字符串中有一些未使用的空间,但对于这样的练习来说,这可能不是什么大问题。如果您要处理数千个字符串,最好声明一个指针数组,然后分别分配每个字符串,这样您只使用与每个字符串相同的内存需要:

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);
完成后,您需要为每个单词释放内存:

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

听起来你只需要一个数组。小心花括号!它们会破坏语法!如果他想修改他的数据呢?他没有说他想修改,我想他不想修改第二对括号内的数据。@JosuéMolina我不太确定,因为他只想比较。@PaulR是的,你是对。我只是不确定OP在测试相等性后为什么分配
inputchar
。这不是数组数组,当然,这是指针数组。但这可能会起作用。很难说-他可能只需要只读数据,但问题不太清楚。要清除它,inputchar是由输入的字符变量用户,这是一个大学生的绞刑练习这太棒了!谢谢你。我只是从指针开始,所以我不太确定,我所做的是实现了你的代码,并将if语句更改为if(inputchar==*word(I)[input],它给了我一个关于调用未定义函数“word”的错误,有什么想法吗?你需要更改word(I)[input]到word[i][input]。已更改,但问题相同:(除了现在未定义的符号外):S@user1842845试试
words[i][input]
如果你复制了史蒂夫的代码。facepalm我觉得自己像个白痴xD万分感谢丹尼尔:)
for (i = 0; i < N; i++)
  free(word[i]);
word[i][input]