Garbage being printed when using strcpy

后端 未结 5 1627
一向
一向 2020-12-12 02:53

I have a function that will parse some data coming in. My problem is that after using strncpy I get some garbage when I try to print it. I try using malloc to make the char

5条回答
  •  忘掉有多难
    2020-12-12 03:18

    One problem: What if there's no newline?

    Undefined behaviour:

    pos = strchr(temp_str, newline);
    strcpy(temp_str, pos+1);
    

    The source and destination of strcpy must not overlap.

提交回复
热议问题