如何输入到数组中

发布于 2024-09-14 11:16:11 字数 258 浏览 11 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独孤求败 2024-09-21 11:16:11

你有两个致命的问题 - 第一个是你需要访问参数数组的 args[i] 成员,第二个是你不能直接分配给 input< /code> 变量,因为它是一个数组。

另外:

  • 您应该检查input数组中有足够的空间;
  • main() 显式返回一个值是一种很好的风格;
  • 空白很便宜,请使用它。

以下是修复这些问题后的样子:(

int main(int c, char **args)
{
    int i;
    char input[100];

    bzero(input, 100);

    for(i = 1; i < c; i++)
    {
        if (strlen(input) + strlen(args[i]) + 2 <= 100)
        {
            strcat(input, args[i]);
            strcat(input, " ");
        }
    }

    puts(input);

    return 0;
}

我还添加了 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 the input variable, since it's an array.

In addition:

  • You should check that there is sufficient room in the input array;
  • It's good style to explicitly return a value from main();
  • Whitespace is cheap, use it.

Here's what it looks like with those issues fixed:

int main(int c, char **args)
{
    int i;
    char input[100];

    bzero(input, 100);

    for(i = 1; i < c; i++)
    {
        if (strlen(input) + strlen(args[i]) + 2 <= 100)
        {
            strcat(input, args[i]);
            strcat(input, " ");
        }
    }

    puts(input);

    return 0;
}

(I also included the puts() line so that you can see what ends up in input).

你另情深 2024-09-21 11:16:11

您已经很接近了 - 尝试 strcat(input, args[i])

You're close - try strcat(input, args[i]).

樱花落人离去 2024-09-21 11:16:11

“char** args”已经是一个字符串数组。这与“char* args[]”是一样的。

"char** args" is already an array of strings. This is the same thing as saying "char* args[]".

你与清晨阳光 2024-09-21 11:16:11

这将解决问题。

  main(int c,char **args){
    int i
    char input[100];
    bzero(input,100);
    for(i=1;i<c;i++)
      {
        strcat(input,args[i]);
        strcat(input," ");
      }
    }

This will solve the problem.

  main(int c,char **args){
    int i
    char input[100];
    bzero(input,100);
    for(i=1;i<c;i++)
      {
        strcat(input,args[i]);
        strcat(input," ");
      }
    }
沉溺在你眼里的海 2024-09-21 11:16:11

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]);

混吃等死 2024-09-21 11:16:11
main(int c, char **args)
{
  while( c-- )
    printf(" %s",*args++);
  return 0;
}
main(int c, char **args)
{
  while( c-- )
    printf(" %s",*args++);
  return 0;
}
弄潮 2024-09-21 11:16:11

如果您不想将程序限制为 100 个字符输入。您可以使用 realloc 动态扩展数组的大小输入

并避免使用 bzero,而是使用作为标准一部分的 memset。即使您可以省略该行。

int main(int c, char **args) {
int i;
int limit = 100;
char *input = calloc(limit,sizeof(limit));

memset(input,0, limit * sizeof(char)); // No need but better than bzero.
for (i=0;i<c;i++) {
    if (strlen(input) + strlen(args[i]) + 2 > limit) { //avoiding input overflow.
       limit = strlen(input) + strlen(args[i]) + 2;
       input = realloc(input,limit);
    }
    strcat(input,args[i]);
    strcat(input," ");
}
puts(input);
free(input);
return 0;

}

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.

int main(int c, char **args) {
int i;
int limit = 100;
char *input = calloc(limit,sizeof(limit));

memset(input,0, limit * sizeof(char)); // No need but better than bzero.
for (i=0;i<c;i++) {
    if (strlen(input) + strlen(args[i]) + 2 > limit) { //avoiding input overflow.
       limit = strlen(input) + strlen(args[i]) + 2;
       input = realloc(input,limit);
    }
    strcat(input,args[i]);
    strcat(input," ");
}
puts(input);
free(input);
return 0;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文