char pointer initialization in C

前端 未结 6 1238
情书的邮戳
情书的邮戳 2021-02-06 18:30

I am not so clear on character pointer and how they work.

The program builds, but crashes when I run it.

 char *ab = NULL;
 //ab = \"abc123\"; // works f         


        
6条回答
  •  花落未央
    2021-02-06 18:58

    "ab" is null and sprintf is trying to write to it, you have to allocate it first.

    char ab[20];
    
    sprintf(ab, "abc%d", 123); //
    

    or

    char * ab = malloc(20); // new, whatever
    
    sprintf(ab, "abc%d", 123); //
    

提交回复
热议问题