传递字符串数组C
我在函数中有以下代码传递字符串数组C,c,arrays,string,pointers,C,Arrays,String,Pointers,我在函数中有以下代码 char MenuOptions[7][200]; strcpy(MenuOptions[0], "Create New / Modify Existing Customer"); strcpy(MenuOptions[1], "Create New / Modify Existing Product"); strcpy(MenuOptions[2], "List All customers"); strcpy(MenuOptions[3], "List All Produ
char MenuOptions[7][200];
strcpy(MenuOptions[0], "Create New / Modify Existing Customer");
strcpy(MenuOptions[1], "Create New / Modify Existing Product");
strcpy(MenuOptions[2], "List All customers");
strcpy(MenuOptions[3], "List All Products");
strcpy(MenuOptions[4], "Batch Update of New Stock");
strcpy(MenuOptions[5], "Create Customer Order");
strcpy(MenuOptions[6], "View Last Order for Customer");
switch (displayMenu("Main Menu", MenuOptions, 7, TRUE)) {
etc....
及
char显示菜单(char*名称,char*选项[],int菜单长度,
enBoolean(已启用){
int i;
printf(“%s:\n”,名称);
对于(i=0;i
但是一旦输入displayMenu方法,MenuOptions数组似乎就从内存中丢失了。我试图通过eclipse中的表达式窗口查看选项[I]是什么,它说它作为一个值“超出范围”在
displayMenu
中的选项类型需要是char(*options)[200]
。不是char*选项[]
除非它是sizeof
\u Alignof
的操作数,或一元&
运算符,或者是用于初始化声明中数组的字符串文字,否则“N元素数组的T
”类型的表达式将被转换(“衰减”)为“指向T
的指针”类型的表达式。在这种情况下,MenuOptions
是一个由200个元素组成的7元素数组,其中包含char
;根据上述规则,它将衰减为“指向char
的200元素数组的指针”类型的表达式
如前所述,options
的类型是char**
;options[i]
为您提供了数组底部后指向char
的i
第200个元素数组的i
指针,而不是char的一个选项:
char displayMenu(char *name, char options[][200], int menuLength, enBoolean QuitEnabled)
更改输入类型:
char displayMenu(char *name, char *options[], ...
要反映它是一个多维数组,请执行以下操作:
char displayMenu(char *name, char options[][200], ...
其他可行的选择:
char displayMenu(char *name, char options[7][200], ...
char displayMenu(char *name, char (*options)[200], ...
主要的一点是当传递多维数组时,所有超过第一个维度的维度也需要传递。首先,打开编译器中的警告选项。编译器应该警告您存在类型不匹配
MenuOptions
是一个由七个200字符数组组成的数组。当调用displayMenu
并将MenuOptions
作为参数传递时,MenuOptions
会自动从数组转换为指向七个200字符数组中第一个数组的指针
在displayMenu
中,使用char*options[]
声明options
参数。这意味着options
是指向char
的指针数组。在函数参数中,[]
是特殊的;它表示将为此参数传递一个数组,但该数组实际上是通过其第一个参数的地址传递的。因此,此声明表示,options
是指向char
的一些指针中的第一个指针
因此,您正在传递一个指向七个200字符数组中第一个数组的指针,但函数希望接收指向char
的一些指针中的第一个的指针
解决此问题的一种方法是将options
的声明更改为char(*options)[200]
。这表示options
是指向200个字符数组(第一个)的指针
更好的修复方法可能是更改菜单选项:
static const char *MenuOptions[] =
{
"Create New / Modify Existing Customer",
"Create New / Modify Existing Product",
"List All customers",
"List All Products",
"Batch Update of New Stock",
"Create Customer Order",
"View Last Order for Customer",
};
(括号中不需要“7”,因为编译器将为您计算字符串。)
那么您不需要更改选项的声明
,除了我添加了常量
:
char displayMenu(char *name, const char *options[], int menuLength,
enBoolean QuitEnabled) {…
现在:
MenuOptions
是一个包含七个字符串指针(字符数组)的数组。它作为指向字符串指针的指针传递,而options
是指向字符串指针的指针,因此类型匹配
MenuOptions
占用的空间不超过所需的空间。以前,每个字符串定义为200个字符。现在,每个字符串只占用所需的空间(加上结尾的空字符和指向MenuOptions
数组中字符串的指针)
- 添加了
const
,以避免意外更改字符串
菜单选项
声明为静态
,因此它在编译时初始化,并且在运行时不需要将字符串复制到其中
最后,您可以替换“7”使用sizeof MenuOptions/sizeof*MenuOptions
调用displayMenu
。这将数组的大小除以数组元素的大小,给出数组中的元素数。这比硬编码常量更可取,因为它消除了有人更改ar大小的可能性不改变硬编码常量的情况下进行渲染。发布etc
,因为您的代码实际上没有任何帮助。为什么不发布实际代码呢?@netcoder,使用ect
似乎完全合适,如果不是更可取的话。他报告的错误似乎是在到达这些行之前发生的。发布到much代码只会让阅读变得更加困难。char*MenuOptions[]={“创建新客户/修改现有客户”、…、“查看客户的上一次订单”};
但您需要发布displayMenu的返回部分…它应该是一个整数值考虑到只传递一个字符串,它会破坏OP的displayMenu
函数,该函数在打印F时循环打印字符串%s
,但是仍然失败。使用(选项)[200]我应该调用选项吗[i] 在printf中,只是为了验证。传递MenuOptions是正确的(而不是&MenuOptions)是吗?@mangusbrother:代码在我上面描述的两种方式下都适用于我。您可能还有另一个错误。为了让人们提供帮助,您必须显示足够完整的代码,以便其他人复制和执行。@mangusbrother:是的,您只需传递菜单选项
,而不是&菜单选项
。它仍然没有打印v最小化代码:displayMenu(char*name,const char*options
char displayMenu(char *name, const char *options[], int menuLength,
enBoolean QuitEnabled) {…