LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to append a character on argv[]? (https://www.linuxquestions.org/questions/programming-9/how-to-append-a-character-on-argv%5B%5D-4175464408/)

georgewhr 06-01-2013 08:43 PM

how to append a character on argv[]?
 
Hello Guys, I m trying to append a character on each argv[].

buffer[256] = "";

for(i = 1; i < argc; i++)
{
strcat(argv[i],";");
strcat(buffer,argv[i]);
}

printf("buffer is %s", buffer);

If I type ./app hello there here, I want to have the buffer which is like hello;there;here

But the code above seems doens't work. Any idea that how can I append a character at the end of each argv?

linosaurusroot 06-01-2013 10:20 PM

You can't add to strings that don't have free space allocated. This should work.
Code:

strcat(buffer,argv[i]);
strcat(buffer,";");


georgewhr 06-02-2013 02:03 AM

Quote:

Originally Posted by linosaurusroot (Post 4963812)
You can't add to strings that don't have free space allocated. This should work.
Code:

strcat(buffer,argv[i]);
strcat(buffer,";");


Thanks bro, butI tried that it doesn't work, if I type "./app george there what",I got print out is:
george;
;
what;

georgewhr 06-02-2013 02:19 AM

Quote:

Originally Posted by linosaurusroot (Post 4963812)
You can't add to strings that don't have free space allocated. This should work.
Code:

strcat(buffer,argv[i]);
strcat(buffer,";");


Hey bro, it works now

thanks!


All times are GMT -5. The time now is 02:55 PM.