如何输入到数组中
main(int c,char **args){
int i
char input[100];
bzero(input,100);
for(i=1;i<c;i++)
{
input=strcat(input,args);
input=strcat(input," ");
}
}
我已经包含了 string.h 头文件....
我希望在命令行中输入的输入存储在输入数组中。 谁能纠正我的代码.. 谢谢。
main(int c,char **args){
int i
char input[100];
bzero(input,100);
for(i=1;i<c;i++)
{
input=strcat(input,args);
input=strcat(input," ");
}
}
i have included the string.h header file....
I want the input i enter in the command line to be stored in the input array.
could anyone please correct my code..
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你有两个致命的问题 - 第一个是你需要访问参数数组的 args[i] 成员,第二个是你不能直接分配给
input< /code> 变量,因为它是一个数组。
另外:
input
数组中有足够的空间;main()
显式返回一个值是一种很好的风格;以下是修复这些问题后的样子:(
我还添加了
puts()
行,以便您可以看到input
中最终出现的内容)。You have two fatal problems - the first is that you need to access the
args[i]
member of the argument array, and the second is that you can't assign directly to theinput
variable, since it's an array.In addition:
input
array;main()
;Here's what it looks like with those issues fixed:
(I also included the
puts()
line so that you can see what ends up ininput
).您已经很接近了 - 尝试
strcat(input, args[i])
。You're close - try
strcat(input, args[i])
.“char** args”已经是一个字符串数组。这与“char* args[]”是一样的。
"char** args" is already an array of strings. This is the same thing as saying "char* args[]".
这将解决问题。
This will solve the problem.
int c 是参数数量加上文件名,c 的值是整数,给出为(传递的参数数量+1)
char **args 包含指向您传递的参数的指针。
您需要更改程序的第 7 行,如下所示;
输入=strcat(输入,args[i]);
int c is the number of arguments plus name of the file that is value of c is integer and given as (number of arguments passed+1)
char **args contain the pointers which points to the arguments you are passing.
you need to change 7 th line of your program as follow;
input=strcat(input,args[i]);
如果您不想将程序限制为 100 个字符输入。您可以使用 realloc 动态扩展数组的大小输入。
并避免使用 bzero,而是使用作为标准一部分的 memset。即使您可以省略该行。
}
In case you do not want to limit your program to 100 chars input. You can use realloc to dynamically extend the size of the array input.
And avoid using bzero, instead use memset that is part of the standard. Even though you could omit that line.
}