c++ sizeof(array) return twice the array's declared length

前端 未结 6 1779
情书的邮戳
情书的邮戳 2020-12-02 02:47

I have a section of code in which two array are declared with sizes of 6 and 13, but when \'sizeof()\' is used the lengths are returned as 12 and 26.

#includ         


        
6条回答
  •  一生所求
    2020-12-02 03:31

    The sizeof operator is measured in units such that an unsigned char is 1 unit.

    On your platform, short is twice as large as char, thus the results you're seeing.

    To properly determine array length, you could use a macro such as:

    #define ARRAY_LEN(ary) (sizeof (ary) / sizeof (ary[0]))
    

提交回复
热议问题