【发布时间】:2015-04-03 22:48:44
【问题描述】:
我要做的是在 C 中访问多维字符串数组中的字符串值。字符串实际上是一个数字值,我想将其存储在整数值中。
当我尝试按以下方式打印值时
printf("TESTING COMMAND\n");
printf("%d\n",commands[0][0]);
值打印正常的期望值
但是,当我尝试使用它来初始化数组时,例如如下
char **options[ (x - 1) ];
这会产生以下错误
error: size of array ‘options’ has non-integer type
这是我所期望的,因为命令数组声明如下
char ***commands;
问题是,即使我尝试分配一个整数变量来保存这个值,我也会遇到分段错误
x = command[0][0];
我也尝试过像strtol 这样的函数,结果相同。但是我不确定我是否正确使用了该功能。
有什么建议吗?
int x;
printf("TESTING COMMAND\n");
printf("%d\n",commands[0][0]);
x = command[0][0];
printf("Creating options of size = %d\n", x );
【问题讨论】:
-
发布更多代码,而不仅仅是小片段
-
printf("%d\n",commands[0][0]);什么是命令,它是如何定义/声明的? -
@wildplasser 命令定义为 char ***commands;
-
@mcleod_ideafix 这是我尝试将整数分配给变量 int x 的代码; printf("测试命令\n"); printf("%d\n",commands[0][0]); x = 命令[0][0]; printf("创建大小选项 = %d\n", x );
-
@Abdel-RahmanShoman 是否有充分的理由以相反的顺序呈现代码?我喜欢从上到下阅读。 (我的编译器也是如此)
标签: c linux string multidimensional-array strtol