Is there a C function that can concatenate all the passed arguments (except the name of the executable) into a char*
and return it?
views:
76answers:
4Strcat in a loop is not so effective, it traverses the first (growing) part of the string for each concatenation.
kaizer.se
2010-06-27 09:33:16
That's hardly likely to be a problem with the typical length of command line parameters...
bdonlan
2010-06-27 17:39:01
A:
Something like this? No guarantees that this will compile.
#include <string.h>
#include <stdio.h>
int main(int argc, char ** argv) {
int i;
int len = 1;
char * str;
for (i = 1; i < argc; i++) {
len += strlen(argv[i]);
}
str = malloc(sizeof(char)*len);
str[0] = '\0';
for (i = 1; i < argc; i++) {
strcat(str, argv[i]);
}
//Use str for whatever you want
printf("My string is %s\n", str);
free(str);
}
Il-Bhima
2010-06-27 09:04:02
You need `str[0] = '\0';` before your `strcat` loop. And your two loops should start at 1 rather than 0 (since he doesn't want the name of the executable in the string). Also it's bad form to cast the result of malloc in C.
Paul R
2010-06-27 09:07:56
Thanks for catching that. Fixed. I was always told its bad form not to cast the result of the malloc. Must google about this.
Il-Bhima
2010-06-27 09:09:00
You should alloc 1 byte more than the length of the sums of string lengths `str = malloc(len+1);` . Besides the OP wanted only the program paramters, so your loops should start with `i=1` not 0.
tristopia
2010-06-27 09:09:18
@Paul R You were right about the casting. It's strange always remember people telling me to cast the malloc. Must be one of those old myths.
Il-Bhima
2010-06-27 09:13:42
Paul mentioned that it's bad form to cast the result of malloc. Besides just looking ugly, it tends to mask the very serious error of using malloc without a prototype, which will break on 64-bit systems. So don't do it.
R..
2010-06-27 09:14:54
@Il-Bhima: you need to do this if you call malloc in C++, unfortunately, but there is no need to in C and omitting the unnecessary casts can help avoid masking bugs that the compiler would otherwise have picked up for you.
Paul R
2010-06-27 09:15:44
Just for my information, why do you cast the return of malloc ? I think I never saw that.EDIT : Ok, didn't see the preivous replies. What kind of reported bug would it "hide" ?
Shelldon
2010-06-27 09:17:05
A:
I don't think there's such a function, but if I'm not wrong, you just have to :
- get the length :
len = strlen(argv[1]) + strlen(argv[2]) + ...
and check for overflow - use malloc :
malloc(len + 1) * sizeof(char))
- set your_copy[0] to '\0'
- use
strcat(your_copy, argv[1])
,strcat(your_copy, argv[2])
... for each remaining argv[]
EDIT : Oh, the previous answer may be better. ;)
Shelldon
2010-06-27 09:09:24
sizeof(*your_copy) is bogus. The definition of sizeof in C is based on char, i.e. sizeof(char) is always 1. By the way, most systems restrict the command line from being gigantic, but you should never add/multiply numbers to pass to malloc unless you've taken care to ensure that the result cannot overflow.
R..
2010-06-27 09:17:40
+2
A:
Try that:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv) {
unsigned int i;
size_t len = 0;
char *_all_args, *all_args;
for(i=1; i<argc; i++) {
len += strlen(argv[i]);
}
_all_args = all_args = (char *)malloc(len+argc-1);
for(i=1; i<argc; i++) {
memcpy(_all_args, argv[i], strlen(argv[i]));
_all_args += strlen(argv[i])+1;
*(_all_args-1) = ' ';
}
*(_all_args-1) = 0;
printf("All %d args: '%s'\n", argc, all_args);
free(all_args);
return 0;
}
Johannes Weiß
2010-06-27 09:14:37