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

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

    The coding style can also be written as :-

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

    But this is done so that the dynamically allocating memory can be increased according to the no of basic data type one wants. if u wanna increase 100 char it will increase 100 char. If may not have need but if we write 101 or 102 doing that would waste memory. doing the it according to the basic data type would not waste any memory space

提交回复
热议问题