Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
传递字符串数组C_C_Arrays_String_Pointers - Fatal编程技术网

传递字符串数组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) {…