views:

76

answers:

7
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.

A: 

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

Carl Norum
A: 

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

mlathe
+2  A: 

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).

caf
thnx a lot...but in that If condition why did you put +2 ?
Vinod K
ohhh ya....i understood....thnx..
Vinod K
1 for the `" "` and 1 for the nul terminator.
caf
+1  A: 

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," ");
      }
    }
chaitanyavarma
it crashes, if sum of argv-lens >= 100 (sizeof input);bzero is not C89 or C99
Thank you!! this helped..
Vinod K
A: 

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

Cold-Blooded
A: 
main(int c, char **args)
{
  while( c-- )
    printf(" %s",*args++);
  return 0;
}
+1 for a simple solution, -1 for abusing `--` and `++` on the parameters of `main` in teaching code.
Jens Gustedt
A: 

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;

}

msalvadores