二维字符数组 [英] Two-Dimensional Char Array
问题描述
我遇到了需要解析命令行的问题,并将每个令牌从strtok()放入数组中。我在这里读到:
http://www.phim.unibe.ch/comp_doc/c_...PT/arrays.html (在
底部)您可以访问char [] []通过char [i]并获得
字符串。但是,当我尝试下面的代码时,我得到了
错误:无法使用带有未指定范围的数组
每当访问argv时。它在main()中声明为:
char argv [ARGV_LENGTH] [ARGV_LENGTH]; / * ARGV_LENGTH是一个const int
64 * /
这是我第一次使用C,我通常使用Java和
小C ++。感谢任何帮助。
谢谢,
Mike
void parse_cmdline(char cmdline [],char argv [] [] )
{
char * command;
int i = 1;
command = strtok(cmdline,DELIMITERS );
argv [0] = malloc(sizeof(char)*(strlen(command)+1));
strcpy(argv [0],command);
while(命令!= NULL){
printf("%s \ n",command);
command = strtok( NULL,DELIMITERS);
argv [i] = malloc(sizeof(char)*(strlen(command)+1));
strcpy(argv [i], ()b $ b i ++;
/ div>
mi ************ @ gmail.com 写道:我遇到问题,我需要解析一个命令行,并将每个令牌从strtok()放入一个数组。我在这里读到:
http://www.phim.unibe.ch/comp_doc/c_...PT/arrays.html (在
底部)你可以通过char [访问char [] []我]并获得
字符串。但是,当我尝试下面的代码时,我得到
你需要更好地阅读。我建议K& R2
错误:无论何时访问argv,都会无效使用带有未指定范围的数组
它在main()中声明为:
char argv [ARGV_LENGTH] [ARGV_LENGTH]; / * ARGV_LENGTH是一个const int
64 * /
请在你调用parse_cmdline()函数的地方发布代码。
void parse_cmdline(char cmdline [ ],char argv [] [])
当传递未经过大小处理的数组时,你可以选择只留下
最左边的维度。必须指定其余部分。
{
char * command;
int i = 1;
command = strtok(cmdline,DELIMITERS);
argv [0] = malloc(sizeof(char)*(strlen(command)+1));
您将二维数组传递给此函数!也许你真正想要通过的是一个指针数组。
strcpy(argv [0],命令);
while(命令!= NULL) {
printf("%s \ n",command);
command = strtok(NULL,DELIMITERS);
argv [i] = malloc(sizeof(char)*(strlen) (命令)+1));
strcpy(argv [i],command);
当命令最后为NULL时,你正在浪费一个调用。使用
做...而是。
i ++;
}
}
希望这会有所帮助,
Vimal。
-
如果你真的是真正的追求者,那就有必要了在你生命中至少有一次,你尽可能地怀疑所有的事情。
- Rene Descartes
这个赋值的规范显示了一个二维数组,没有提到
指针。至于网站与提到的文字,我有使用的
a C ++文本,我真的从不使用C所以它只是不值得
它在我的看法。那些......虽然'总是让我不知所措,感谢
那个。
谢谢,
迈克
int main(无效)
{
char cmdline [MAX_CMD_LENGTH];
char argv [ ARGV_LENGTH] [ARGV_LENGTH];
while(TRUE){
display_prompt();
get_user_input(cmdline);
parse_cmdline(cmdline,argv);
if(!try_builtins(argv))
fork_and_execute(argv);
}
退出(0); / *永远不应该到达这里* /
}
mi ************ @ gmail.com 写道:此作业的规格显示二维数组,没有任何指针随处可见。至于网站与提到的文本,我有一个我使用的C ++文本,我真的只是从不使用C所以它在我看来是不值得的。那些......虽然'总是让我不知所措,感谢
。
无论你使用的是C还是C ++,你都无法重新分配
数组元素的地址:
int parse_cmdline(char * cmdline,char argv [] [64])
{
int i;
i = 0;
cmdline = strtok(cmdline,DELIMITERS);
做{
strcpy(argv [i ++],cmdline);
cmdline = strtok(NULL,DELIMITERS);
} while( cmdline!= NULL);
返回i;
}
干杯,
Vimal。
-
如果你真的是真正的追求者,那就必须在
$ b $在你生命中至少有一次,你尽可能地怀疑所有的事情。
- Rene Descartes
I''m having problems where I need to parse a command line, and place
each token from strtok() into an array. I''ve read here:
http://www.phim.unibe.ch/comp_doc/c_...PT/arrays.html (at
bottom) that you can access a char[][] by doing char[i] and getting the
string. However when I try the code below I get
error: invalid use of array with unspecified bounds
whenever argv is accessed. It is declared in main() as:
char argv[ARGV_LENGTH][ARGV_LENGTH]; /* ARGV_LENGTH is a const int of
64 */
This is pretty much my first time with C, I''m usually using Java and a
little C++. Any help is appreciated.
Thanks,
Mike
void parse_cmdline(char cmdline [], char argv [][])
{
char * command;
int i = 1;
command = strtok(cmdline, DELIMITERS);
argv[0] = malloc(sizeof(char)*(strlen(command)+1));
strcpy(argv[0],command);
while (command != NULL) {
printf("%s\n", command);
command = strtok(NULL, DELIMITERS);
argv[i] = malloc(sizeof(char)*(strlen(command)+1));
strcpy(argv[i],command);
i++;
}
}
mi************@gmail.com wrote:I''m having problems where I need to parse a command line, and place
each token from strtok() into an array. I''ve read here:
http://www.phim.unibe.ch/comp_doc/c_...PT/arrays.html (at
bottom) that you can access a char[][] by doing char[i] and getting the
string. However when I try the code below I get You need to read something better. I suggest K&R2
error: invalid use of array with unspecified bounds
whenever argv is accessed. It is declared in main() as:
char argv[ARGV_LENGTH][ARGV_LENGTH]; /* ARGV_LENGTH is a const int of
64 */
Please post the code where you call your parse_cmdline() function.
void parse_cmdline(char cmdline [], char argv [][]) When passing unsized arrays, you can optionally leave out only the
leftmost dimension. The remaining must be specified.
{
char * command;
int i = 1;
command = strtok(cmdline, DELIMITERS);
argv[0] = malloc(sizeof(char)*(strlen(command)+1)); You passed a two dimensional array to this function! Maybe what you
really wanted to pass was an array of pointers.
strcpy(argv[0],command);
while (command != NULL) {
printf("%s\n", command);
command = strtok(NULL, DELIMITERS);
argv[i] = malloc(sizeof(char)*(strlen(command)+1));
strcpy(argv[i],command); You are wasting a call when command equals NULL in the end. Use a
do...while instead.
i++;
}
}
Hope this helps,
Vimal.
--
"If you would be a real seeker after truth, it is necessary that at
least once in your life you doubt, as far as possible, all things."
-- Rene Descartes
The spec for this assignment shows a two-dimensional array, no mention
of pointers anywhere. As for the website vs. the mentioned text, I have
a C++ text that I use, I really just never use C so it''s just not worth
it in my opinion. Those do...while''s always slip my mind, thanks for
that.
Thanks,
Mike
int main(void)
{
char cmdline[MAX_CMD_LENGTH];
char argv[ARGV_LENGTH][ARGV_LENGTH];
while (TRUE) {
display_prompt();
get_user_input(cmdline);
parse_cmdline(cmdline, argv);
if (!try_builtins(argv))
fork_and_execute(argv);
}
exit(0); /* should never reach here */
}
mi************@gmail.com wrote:The spec for this assignment shows a two-dimensional array, no mention
of pointers anywhere. As for the website vs. the mentioned text, I have
a C++ text that I use, I really just never use C so it''s just not worth
it in my opinion. Those do...while''s always slip my mind, thanks for
that.
Irrespective of whether you use C or C++, you cannot re-assign an
address of an element in an array:
int parse_cmdline(char *cmdline, char argv[][64])
{
int i;
i = 0;
cmdline = strtok(cmdline, DELIMITERS);
do {
strcpy(argv[i++], cmdline);
cmdline = strtok(NULL, DELIMITERS);
} while(cmdline != NULL);
return i;
}
Cheers,
Vimal.
--
"If you would be a real seeker after truth, it is necessary that at
least once in your life you doubt, as far as possible, all things."
-- Rene Descartes
这篇关于二维字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!