使用字符串二维数组 [英] 2D array using strings

查看:127
本文介绍了使用字符串二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我卡在一些功课未分级(其意味着实践)。

I'm stuck on some homework which isn't graded (its meant for practice).

我要创建一个名为 find_name 函数需要两个参数。第一个参数是名称(字符串)的二维数组,第二个是用于发现二维数组中的名称的字符串,该函数必须返回1,如果别人发现了0。

I have to create a function called find_name that 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_name'的参数1

When i call the function (which is empty right now), I get this warning: passing argument 1 of 'find_name' from incompatible pointer type

下面是最重要的位。

在主

 char strNameList[][2] = { { "Luca","Daniel"} ,{"Vivan","Desmond"},{"Abdul","Justin"}, {"Nina","Marlene"},{"Donny","Kathlene"} };

char strFindName[] = "\0";
    printf("Please enter a name to look for: ");
    gets(strFindName);
    nSearch = find_name(strNameList, strFindName);

的功能

int find_name(char strNameList[][2], char strLookUp[])

我是新的C(我是一个学生),我完全糊涂了关于字符串(字符串数组等等)。

I'm new to C (I'm a student), and I'm completely confused about strings (string arrays etc).

推荐答案

我假设你想字符指针的二维数组。您strNameList的声明是不正确的的在程序中既的位置。您有:

I'm assuming you want a 2D array of char pointers. Your declaration of strNameList is incorrect in both locations in your program. You have:

char strNameList[][2] = { { "Luca","Daniel"} ,{"Vivan","Desmond"},{"Abdul","Justin"}, {"Nina","Marlene"},{"Donny","Kathlene"} };

的char [] [N] 是声明的二维数组的 字符 的,不是的char * 因此你被你给类型的项目分配指针值筏编译器警告字符

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

修改的两个的你的声明(您的变量的的你的函数参数):

Change both your declarations (your variable and your function parameter) to:

const char *strNameList[][2]

该声明的两个的char *数组的长度未知的数组,现在符合您的初始化列表。此外,常量添加,因为(a)我假设你是不是修改你的函数名称列表规划,以及(b)分配给可写的字符串声明的char * 通过初始化是在C未定义的行为,并在C ++中,pcated所以你不应该使用它,无论正式去$ p $。同样,您的查找名可能不被任何修改,所以也将其声明常量

which declares an array of unknown length of arrays of two char*, which now matches your initialization lists. Also, the const is 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.

结果:

const char * strNameList[][2] = { 
    {"Luca","Daniel"} ,
    {"Vivan","Desmond"},
    {"Abdul","Justin"}, 
    {"Nina","Marlene"},
    {"Donny","Kathlene"} 
};

和你的功能:

int find_name(const char * strNameList[][2], const char strLookUp[])

最后但并非最不重要的,除非你有一个水晶球你的 find_name()函数没有用给定的信息办法知道的有多少名是传递名单的。我宁愿你看到现在,而不是想知道后来发生了什么事。你需要(a)用令牌值 find_name()知道,或(b)通过名称的数量在列表中<$结束列表C $ C> find_name()。每一个自己,但我preFER这些后者:

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:

int find_name(const char * strNameList[][2], size_t nNameListSize, const char strLookUp[])

和调用它在你的发送方:

and invoke it on your caller side by:

find_name(strNameList, sizeof(strNameList)/sizeof(strNameList[0]), strFindName)

这篇关于使用字符串二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆