问题
I got a problem with C programming string concatenation.
Why strcat(dest, "\something")
will not have the backslash copied to dest
?
You may wish to follow the example.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *dest = "";
char *test = "how \something";
dest = (char *)malloc((strlen(test) + 1) * sizeof(char));
strcat(dest, test);
// Expect to see "how \something", but showed "how something"
printf("%s\n", dest);
free(dest);
return 0;
}
回答1:
backslash not showing
Escape it:
char *test = "how \\something";
The compiler should have warned you about this like this (GCC 4.9.2):
main.c:8:16: warning: unknown escape sequence: '\s'
char *test = "how \something";
Also this is not related to strcat()
.
To prove this just do
puts(test);
and receive the same output.
but showed "how something"
Are you sure? I'd expected
how omething
Also
sizeof (char)
is 1 by definition.- In C there is no need to cast
void
-pointers as for example returned bymalloc()
, nor is it recommended in any way.
So just do
dest = malloc(strlen(test) + 1);
or so make the code safe against late changes of dest
's type do
dest = malloc((strlen(test) + 1) * sizeof *dest);
Addition
As pointed out by anatolyg in this comment you are concatenating to an uninitialised char
-array here:
strcat(dest, test);
This invokes undefined behaviour, which is not good.
To fix this either do
dest[0] = '\0';
or- use
calloc()
instead ofmalloc()
to 0
-terminate dest
and with this make the char
-array a C-"string" before using it alike when passing it to strcat()
.
- Or just use
strcpy()
instead ofstrcat()
.
回答2:
There are 2 separate problems in your code:
You have an unsupported escape sequence in the string
"how \something"
.\
followed bys
is not recognised by the compiler and is interpreted ass
, like\"
and\'
would be interpreted as"
and'
respectively. To include a\
in a string constant, you should escape it as\\
.You concatenate
test
todest
withstrcat(dest, test);
butdest
points to an uninitialized block of memory allocated bymalloc()
. You should usestrcpy()
for that. Note that you could just use the POSIX functionstrdup()
, if available on your system.
Less important, but worth considering:
- there is no need to cast the return value of
malloc()
in C (unlike C++). - you should test the return value of
malloc()
. - you should
const
qualify pointers pointing to string constants. It is not strictly necessary but good practice to avoid various kinds of bugs. - the value of
sizeof(char)
equals 1 by definition.
Here is a simplified and corrected version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char *test = "how \\something";
char *dest = malloc(strlen(test) + 1);
if (dest != NULL) {
strcpy(dest, test);
printf("%s\n", dest);
free(dest);
}
return 0;
}
回答3:
As @melepmene mentioned in comments, "This has nothing to do with strcat
and everything with the C syntax for string literals."
To make you understand the problem with your code, I'm gonna answer your question with another question.
Expect what's the output of the following program, execute the program and check the output (obviously it'd be different from what you've expected if it isn't then you'd have not asked this question in first place) and then go and google "C syntax for string literals"
int main()
{
printf("%s\n", "\how \is?");
return 0;
}
来源:https://stackoverflow.com/questions/45400859/strcatdest-something-backslash-not-showing