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

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

    Think of unicode and multi-byte strings. If char represents single character in a string, it can actually occupy more than one byte, resulting in sizeof() > 1

提交回复
热议问题