3

I am coming from java, and I cannot figure this out. I am trying to concatenate strings to run a command with parameters using the system function, this is what I am doing, but it is not working:

system("command "+param1+" other stuff "+param3);
user2484067
  • 109
  • 1
  • 3
  • 10
  • 2
    Why the downvotes? This is a perfectly reasonable question. – James M Jun 30 '13 at 14:35
  • @JamesMcLaughlin Because any reasonable beginner-level C tutorial points out that concatenating strings is not *that* easy in C, and that it's done using stdlib functions. –  Jun 30 '13 at 14:38

3 Answers3

6

To concatenate string in C use snprintf

In C strings are representing as a array of chars. Their name is pointer to their first element. Every operation should be done using functions. In other case you simply make pointer arithmetic. So it your example you try to launch function from address that probably doesn't even exist in system.

Do not use strcat! It is dangerous If you really need simple function to call use strlcat

For more information you need back to basics

janisz
  • 6,292
  • 4
  • 37
  • 70
  • 4
    `strcat` can lead to buffer overflow, better to use `snprintf` in this case I think. – Some programmer dude Jun 30 '13 at 14:32
  • 4
    **No!** No! Not `strcat`! `strcat` dispenses buffer overruns like the Sun dispenses sunlight! ...seriously, ***don't***. – michaelb958--GoFundMonica Jun 30 '13 at 14:33
  • 2
    Just dimension your buffer properly and `strcat` will be fine. – James M Jun 30 '13 at 14:34
  • @JamesMcLaughlin: the problem is that it becomes quickly nontrivial and error-prone if you have to append more than just one string. Better to use a function like `snprintf` that does the math on your behalf. – Matteo Italia Jun 30 '13 at 14:35
  • @MatteoItalia Add the lengths of your strings together + 1 for the null terminator. That's not too hard. – James M Jun 30 '13 at 14:36
  • 1
    @JamesMcLaughlin: again, it's all fine and good when you have to happen just one string, it becomes a mess of `strlen` and off-by-one errors when you start to have more stuff to concatenate (also, it's inefficient because every `strcat` and `strlen` walk all the string every time); as said above, leave this mess to `snprintf`. – Matteo Italia Jun 30 '13 at 14:39
3

Probably you want to build your command string with a function like snprintf: you have the ability to insert in your string various types (not only strings) and you are safeguarded against buffer overflows (check its return value!).

char buffer[256];
if(snprintf(buffer, sizeof(buffer), "command %s other stuff %s", param1, param3)>=sizeof(buffer))
{
   /* the buffer isn't big enough */
}
else
    system(buffer);
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Ok, just wondering is the %s where the params will go into the command? – user2484067 Jun 30 '13 at 14:45
  • Read [here](http://www.cplusplus.com/reference/cstdio/printf/) about possible format. No idea why don't link it to other string functions – janisz Jun 30 '13 at 14:49
  • @user2484067: yes, that's the placeholder for string parameters; if you wanted to insert integers instead you'd use `%d`, for floating point types `%f`, etc, you find all the supported field specifiers in the [documentation](http://www.cplusplus.com/reference/cstdio/printf/). – Matteo Italia Jun 30 '13 at 14:50
0

There is no operator overloading in c. You have to create enough buffer and then use library function strcat.

Mahesh
  • 34,573
  • 20
  • 89
  • 115