'strcpy' with 'malloc'?

后端 未结 6 1651
予麋鹿
予麋鹿 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:24

    Use:

    #define MYSTRDUP(str,lit) strcpy(str = malloc(strlen(lit)+1), lit)
    

    And now it's easy and standard conforming:

    char *s;
    MYSTRDUP(s, "foo bar");
    

提交回复
热议问题