This question already has an answer here:
I know I shouldn't be using that function, and I don't care. Last time I checked the spec for strcat, it said something along the lines of updating the first value as well as returning the same.
Now, this is a really stupid question, and I want you to explain it like you're talking to a really stupid person.
Why won't this work?
char* foo="foo";
printf(strcat(foo,"bar"));
EDIT: I don't know the difference between char[] and char*. How would I allocate a string of 255 characters?
EDIT 2: OK, OK, so char[number] allocates a string of that many bytes? Makes sense. Thanks.
EDIT 3: Also, how would I use a character array without declaring it? Would I typecast it as char[255]?
EDIT 4: strcat((char[256])"foo","bar") returns an error. I'm about fed up with C.
EDIT 5: So does strcat((char[256])"foo",(char[])"bar").
EDIT 5:
char[256] foo="bar";
Real smooth. "identifier expected"
You need:
String constants provide only the exact amount of memory in the literal and they aren't supposed to be written.
Instead, do:
strcat
is pretty simple -- it takes pointer to a buffer containing a string and pointer to another string, and copies that second string into the buffer at the end of the string that's already there.Note the difference between the two arguments. Both are
char *
, but the first is really a pointer to a buffer and only incidentally a pointer to a string (thats already in the buffer). As a buffer, it needs two things that a simple string does not:In your example, you try to use
char *foo="foo";
as the first argument, but its just a string and is missing both of the requirements for the buffer. Instead you need to do something like:Now we're declaring
foo
as a writable char array with plenty of space to hold both strings -- an adequate buffer. This gets into the problem that most people have with strcat, which is the second requirement above -- how do you know if the buffer has enough space? In a simple example like this it does, but for more complex examples (where you don't necessarily know how long the strings are) it gets much harder. You end up needing to keep track of how big your buffer is, and use strlen to see how long the strings are to make sure they'll fit BEFORE you call strcat. This is both inefficient (you end up scanning over string strings multiple times to find lengths and then to copy) and error prone.A few problems...
1.
foo
is constant here. You cannot modify string literals.2. The contract for
strcat
is that the first parameter is large enough to fit the concatenated string. So more realistically you'd do something this...3. As you might guess, it's not obvious to a newcomer how this is supposed to work. I say there's a reason for this: in general
strcat
is considered quite bad. A good C interface for interacting with buffers of potentially arbitrary length will make this a lot more explicit. You might want to look atstrncat
orstrlcat
which track sizes.I would say in general though if you're using the
strcat
family you're doing something wrong. Each call tostrcat
will have to traverse the string to find where the end is. Imagine you are doing a lot of these operations -- it's very easy to imagine something that can easily be done in O(n) steps suddenly turning into O(n2) because of the repeated traversal of the string. If you need to concatenate to a string repeatedly, you should be maintaining a pointer to the current end of the string and doing the copy from there.Update: Example of how you might do this last suggestion follows...
Then you might use this as follows:
is a string literal.
It can't be modified. Do
char [] foo = "foo";
instead but keep in mind that using strcat like that, will cause problems in this case cause it will write in memory it shouldn't when trying tostrcat
"bar" so you should try something likechar foo[30] = "foo";
Edit: The typecasting you do... sorry I do not have so many brain cells as to try to explain you what you are trying to do. I can only tell you it is wrong. you need to provide a memory location so
strcat()
can work.Try that :