我对 C++ 编程非常陌生,你会明白为什么。
我想制作一个由几个单词组成的字符数组,我想使用线性搜索函数进行搜索。这个数组必须是二维数组吗?例如:
char Colors[3][6] = {"red", "green", "blue"};
我试过这样:
char Colors[] = {"red", "green", "blue"};
这给了我一个“太多的初始化程序”错误。
我假设第一种方法是正确的,因为它说明了数组中元素的数量和元素的最大长度,对吗?
现在我将如何实现一个线性搜索函数来在该数组中查找一个单词?我可以执行以下操作:
(假设已经声明了linearSearch函数)
char searchKey;
char element;
char Colors[3][6] = {"red", "green", "blue"};
printf("Enter the color to look for: \n");
scanf("%s", searchKey);
element = linearSearch(Colors, searchKey, ??); //?? is where I don't know what to enter
if (element != -1)
{
printf("Found the word.\n");
}
else
{
printf("Didn't find the word.\n");
}
这可能吗?如果是这样,该声明将寻找什么 linearSearch 函数?我希望我提供了足够的信息以使其有点可用。
编辑:感谢大家的帮助,让程序按预期工作。