Why do we need to cast what malloc returns?

后端 未结 7 1866
情歌与酒
情歌与酒 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:21

    The C FAQ list is an invaluable resource: Why does some code carefully cast the values returned by malloc to the pointer type being allocated?.

提交回复
热议问题