How Does sizeof(Array) work

后端 未结 5 974
我在风中等你
我在风中等你 2020-11-27 15:42

How does c find at run time the size of array? where is the information about array size or bounds of array stored ?

5条回答
  •  一向
    一向 (楼主)
    2020-11-27 16:16

    sizeof gives the size of the variable, not the size of the object that you're pointing to (if there is one.) sizeof(arrayVar) will return the array size in bytes if and only if arrayVar is declared in scope as an array and not a pointer.

    For example:

    char myArray[10];
    char* myPtr = myArray;
    
    printf("%d\n", sizeof(myArray)) // prints 10 
    printf("%d\n", sizeof(myPtr)); // prints 4 (on a 32-bit machine)
    

提交回复
热议问题