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

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

    I do sizeof(char) to make the intentions clear. If anyone ever decides he wants foo to be an int he knows he'll need to do sizeof(int) for it to keep on working.

    or omit it and use the number

    Plus it's not very good coding practice to use magic numbers.

提交回复
热议问题