Sizeof arrays and pointers

后端 未结 5 563
小蘑菇
小蘑菇 2020-11-30 15:20

Here is my sample code

#include
void main()
{
 int arr[]={1,2,3,4,5,6};
 char *ptr,a;
 a=\'c\';
 ptr=&a;
 int *ptr1,a1;
 a1=4;
 ptr1=&         


        
5条回答
  •  独厮守ぢ
    2020-11-30 16:19

    the sizeof() operator does not give you the number of elements in an array, it gives you the number of bytes a thing occupies in memory. Hence:

    #include 
    
    int main()
    {
      char s[] = { 1, 2, 3, 4, 5, 0 };
      int xs[] = { 1, 2, 3, 4, 5, 0 };
    
      printf( "sizeof( s ) = %d\n",  sizeof( s  ) );
      printf( "sizeof( xs ) = %d\n", sizeof( xs ) );
    
      return 0;
    }
    
    sizeof( s ) = 6
    sizeof( xs ) = 24
    

提交回复
热议问题