char pointer initialization in C

前端 未结 6 1234
情书的邮戳
情书的邮戳 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 19:08

    You need to allocate memory for your data. Indeed sprintf takes char*, but it doesn't allocate memory for you.

    The first line works fine because compiler automatically allocates data for constant tables of chars defined at compile time.

提交回复
热议问题