strcat(dest, “\something”) - backslash not showing

孤人 提交于 2019-12-12 04:58:02

问题


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 by malloc(), 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 of malloc()

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 of strcat().



回答2:


There are 2 separate problems in your code:

  • You have an unsupported escape sequence in the string "how \something". \ followed by s is not recognised by the compiler and is interpreted as s, like \" and \' would be interpreted as " and ' respectively. To include a \ in a string constant, you should escape it as \\.

  • You concatenate test to dest with strcat(dest, test); but dest points to an uninitialized block of memory allocated by malloc(). You should use strcpy() for that. Note that you could just use the POSIX function strdup(), 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!