Why write `sizeof(char)` if char is 1 by standard?

前端 未结 12 1742
情书的邮戳
情书的邮戳 2021-01-31 07:26

I was doing some C coding and after reading some C code I\'ve noticed that there are code snippets like

char *foo = (char *)malloc(sizeof(char) * someDynamicAmo         


        
12条回答
  •  星月不相逢
    2021-01-31 08:28

    IMHO the best practice is to write sizeof(*foo). Then you're covered also if the type of foo changes and the sizeof is not corrected.

提交回复
热议问题