Why sizeof(array) and sizeof(&array[0]) gives different results?

前端 未结 8 966
悲&欢浪女
悲&欢浪女 2020-12-30 06:56
#include 
int main(void){
    char array[20];

    printf( \"\\nSize of array is %d\\n\", sizeof(array) );  //outputs 20
    printf(\"\\nSize of &         


        
相关标签:
8条回答
  • 2020-12-30 07:47

    sizeof(array) is giving you size of total array and sizeof(&array[0]) is giving you sizeof(char *)

    Your question is perfect example of Array and pointer are not same. Array may act as pointer.Have a look here.

    0 讨论(0)
  • The variable array is an array of 20 char, the value of the expression sizeof(array) is equal to the sizeof bytes of an array of 20 chars, which is 20.

    &array[0] is a pointer. The sizeof returns the size of a pointer, which is 4 in your machine.

    0 讨论(0)
提交回复
热议问题