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

前端 未结 12 1749
情书的邮戳
情书的邮戳 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:16

    The more Cish way would be

    char* foo = malloc(someDynamicAmount * sizeof *foo);
    

    referencing the variable and not the type so that the type isn't needed. And without casting the result of malloc (which is C++ish).

提交回复
热议问题