Why do we need to cast what malloc returns?

后端 未结 7 1876
情歌与酒
情歌与酒 2020-12-10 18:04
    int length = strlen(src);
    char *structSpace = malloc(sizeof(String) + length + 1);
    String *string = (String*) structSpace;    
    int *string = (int*) s         


        
7条回答
  •  孤街浪徒
    2020-12-10 18:19

    You should strongly consider casting after using the malloc command because it provides for greater portability and greater compatibility with other parts of your program. If you do not do so, you may run the risk of incompatible data types which could result in errors.

提交回复
热议问题