What is the trick behind strcpy()/uninitialized char pointer this code?

后端 未结 3 1298
长发绾君心
长发绾君心 2021-01-23 01:48
#include 
#include 
#include 

void main ()
{
  char *imsi;
  unsigned int i;
  int val;
  char *dest;

  imsi = \"4057501         


        
3条回答
  •  無奈伤痛
    2021-01-23 02:20

    The behaviour of this code is unpredictable because the pointer dest is used before it is initialised. The difference in observed behaviour is only indirectly related to the root cause bug, which is the uninitialised variable. In C it is the programmers responsibility to allocate storage for the output of the strncpy() function and you haven't done that.

    The simplest fix is to define an output buffer like this: char dest[10];

提交回复
热议问题