'strcpy' with 'malloc'?

后端 未结 6 1649
予麋鹿
予麋鹿 2020-12-09 10:29

Is it safe to do something like the following?

#include 
#include 
#include 

int main(void)
{
    char* msg;
         


        
6条回答
  •  既然无缘
    2020-12-09 11:10

    The first version is not safe. And, msg should be pointing to valid memory location for "Hello World!!!" to get copied.

    char* msg = (char*)malloc(sizeof(char) * 15);
    strcpy(msg, "Hello World!!!");
    

提交回复
热议问题