3

I am trying to concat two const char * strings.

When i have a statement like strcat(a,b) I get the warning expected ‘char * restrict’ but argument is of type ‘const char *’

is there a way to call strcat that will not produce the warning? Thanks!

user974703
  • 1,653
  • 4
  • 20
  • 27

1 Answers1

8

strcat() modifies the first operand. Therefore it cannot be const. But you passed it a const char*.

So you can't use strcat() on two const *char strings.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • great thanks! It led to some ugly statements, but works just fine, instead of saying `char * c = strcat(a,b); ` I can use `char * c = strcat(strcat(c,a),b);` provided I've malloc'd c already. – user974703 Feb 13 '12 at 21:22
  • @user974703: You'd better be sure that you `malloc` *and* set to zero the memory pointed to by `c`, because `malloc` doesn't do that for you. Or, use `strcat(strcpy(c, a), b);`. Or, since `strcat` isn't very efficient, `sprintf(c, "%s%s", a, b);` – Greg Hewgill Feb 13 '12 at 21:52