C array declaration and assignment?

前端 未结 7 1520
囚心锁ツ
囚心锁ツ 2020-11-29 07:59

I\'ve asked a similar question on structs here but I\'m trying to figure out how C handles things like assigning variables and why it isn\'t allowed to assign them to eachot

7条回答
  •  粉色の甜心
    2020-11-29 08:19

    Some messages here say that the name of an array yields the address of its first element. It's not always true:

    #include 
    
    int
    main(void)
    {
      int array[10];
    
      /*
       * Print the size of the whole array then the size of a pointer to the
       * first element.
       */
      printf("%u %u\n", (unsigned int)sizeof array, (unsigned int)sizeof &array[0]);
    
      /*
       * You can take the address of array, which gives you a pointer to the whole
       * array. The difference between ``pointer to array'' and ``pointer to the
       * first element of the array'' matters when you're doing pointer arithmetic.
       */
      printf("%p %p\n", (void*)(&array + 1), (void*)(array + 1));
    
      return 0;
    }
    

    Output:

    40 4
    0xbfbf2ca4 0xbfbf2c80
    

提交回复
热议问题