C语言 使用字符串的二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13501579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
2D array using strings
提问by Luca Tenuta
I'm stuck on some homework which isn't graded (its meant for practice).
我被困在一些没有评分的作业上(这是为了练习)。
I have to create a function called find_namethat takes 2 arguments. The first argument is a 2D array of names (strings), and the second is a character string which is used to find the name in the 2D array, the function must return 1 if found else 0.
我必须创建一个带有find_name2 个参数的函数。第一个参数是名称(字符串)的二维数组,第二个参数是用于在二维数组中查找名称的字符串,如果找到,则函数必须返回 1,否则返回 0。
When i call the function (which is empty right now), I get this warning: passing argument 1 of 'find_name' from incompatible pointer type
当我调用函数(现在是空的)时,我得到了这个 warning: passing argument 1 of 'find_name' from incompatible pointer type
Here is the important bits.
这是重要的部分。
In Main
在主要
char strNameList[][2] = { { "Luca","Daniel"} ,{"Vivan","Desmond"},{"Abdul","Justin"}, {"Nina","Marlene"},{"Donny","Kathlene"} };
char strFindName[] = "int find_name(char strNameList[][2], char strLookUp[])
";
printf("Please enter a name to look for: ");
gets(strFindName);
nSearch = find_name(strNameList, strFindName);
The Function
功能
char strNameList[][2] = { { "Luca","Daniel"} ,{"Vivan","Desmond"},{"Abdul","Justin"}, {"Nina","Marlene"},{"Donny","Kathlene"} };
I'm new to C (I'm a student), and I'm completely confused about strings (string arrays etc).
我是 C 的新手(我是学生),我对字符串(字符串数组等)完全感到困惑。
回答by WhozCraig
I'm assuming you want a 2D array of char pointers. Your declaration of strNameList is incorrect in bothlocations in your program. You have:
我假设您想要一个 2D 字符指针数组。您对 strNameList 的声明在程序的两个位置都不正确。你有:
const char *strNameList[][2]
But char[][N]is declaring a 2D array of chars, not char*Therefore you're being warned by the compiler you're assigning a raft of pointer values to items of type char
但是char[][N]声明了一个 2D 数组chars,而不是char*因此编译器警告您正在将大量指针值分配给类型项char
Change bothyour declarations (your variable andyour function parameter) to:
更改既您的声明(你的变量和你的函数参数):
const char * strNameList[][2] = {
{"Luca","Daniel"} ,
{"Vivan","Desmond"},
{"Abdul","Justin"},
{"Nina","Marlene"},
{"Donny","Kathlene"}
};
which declares an array of unknown length of arrays of two char*, which now matches your initialization lists. Also, the constis added because (a) I'm assuming you are not planning on modify that name list in your function, and (b) writable string literal declarations assigned to char*via initializer is undefined behavior in C, and officially deprecated in C++, so you should not be using it regardless. Likewise, your lookup-name is probably not being modified either, so also declare it const.
它声明了一个长度未知的两个数组char*,它现在与您的初始化列表相匹配。此外,const添加 是因为(a)我假设您不打算在函数中修改该名称列表,并且(b)分配给char*通过初始化程序的可写字符串文字声明在 C 中是未定义的行为,并且在 C++ 中正式弃用,所以无论如何你都不应该使用它。同样,您的查找名称也可能没有被修改,因此也声明它const。
Result:
结果:
int find_name(const char * strNameList[][2], const char strLookUp[])
and in your function:
并在您的功能中:
int find_name(const char * strNameList[][2], size_t nNameListSize, const char strLookUp[])
Last but certainly not least, unless you have a crystal ball your find_name()function has no way of knowing with the given information how many names are in the name list being passed. I'd rather you see this now rather than wonder what happened later. you need to either (a) terminate the list with a token-value that find_name()knows about, or (b) pass the number of names in the list to find_name(). To each their own, but I prefer the latter of these:
最后但同样重要的是,除非您有一个水晶球,否则您的find_name()函数无法通过给定的信息知道正在传递的名称列表中有多少名称。我宁愿你现在看到这个,而不是想知道后来发生了什么。您需要 (a) 使用find_name()已知的令牌值终止列表,或 (b) 将列表中的名称数量传递给find_name(). 每个人都有自己的,但我更喜欢后者:
find_name(strNameList, sizeof(strNameList)/sizeof(strNameList[0]), strFindName)
and invoke it on your caller side by:
并通过以下方式在您的呼叫方调用它:
#define STOPPER_NAMELIST NULL
char * strNameList[][2] = {
{ "Luca","Daniel"},
{"Vivan","Desmond"},
{"Abdul","Justin"},
{"Nina","Marlene"},
{"Donny","Kathlene"}
{STOPPER_NAMELIST, STOPPER_NAMELIST}
};
size_t sizeNameList(const char * strNameList[][2])
{
size_t size = 0;
while ((strNameList[size][0] != STOPPER_NAMELIST) &&
(strNameList[size][0] != STOPPER_NAMELIST))
++ size;
return size;
}
int find_name(char * strNameList[][2], char strLookUp[])
{
size_t size = sizeNameList(strNameList);
...
}
...
nSearch = find_name(strNameList, strFindName);
回答by alk
Do it this way:
这样做:
char arr[][2] = { { 'a', 'b'}, ...
This approach uses an open array ([]) of char *arrays with 2entries.
此方法使用带有条目[]的char *数组的开放数组 ( ) 2。
Update:
更新:
You could add a stopper element to the array carring the names, then there is no need to pass around the array's size along with array itself, as the size could alway be determined by scanning the array members until the stopper is found.
您可以向带有名称的数组添加一个停止元素,然后就不需要传递数组的大小以及数组本身,因为大小始终可以通过扫描数组成员来确定,直到找到停止元素。
回答by Mike
Your function find_name()is looking for a 2-D array of characters ie:
您的函数find_name()正在寻找一个二维字符数组,即:
char *arr[][2] = { {"John", "Smith"}, ...
if you want to make them stringsyou need:
如果你想让它们成为你需要的字符串:
void find_name(char *something[][2])
{
printf("first name: %s, second name: %s\n", something[0][0], something[0][1]);
Then in the function parameter list you need:
然后在函数参数列表中你需要:
find_name(arr);
And in your main()function call it just by:
在您的main()函数中,只需通过以下方式调用它:

