Difference between *ptr[10] and (*ptr)[10]

前端 未结 6 1408
自闭症患者
自闭症患者 2020-12-04 17:29

For the following code:

    int (*ptr)[10];
    int a[10]={99,1,2,3,4,5,6,7,8,9};
    ptr=&a;
    printf(\"%d\",(*ptr)[1]);

What should

6条回答
  •  抹茶落季
    2020-12-04 17:37

    int(*)[10] is a pointer to an int array with 10 members. i.e it points to int a[10].

    where as int *[10] is array of integer pointers

    #include 
    int main()
    {
    
    int *ptr[10];
    int a[10]={0,1,2,3,4,5,6,7,8,9};
    
    printf("\n%p  %p", ptr[0], a);
    
    *ptr=a; //ptr[0] is assigned with address of array a.
    
    printf("\n%p  %p", ptr[0], a); //gives you same address
    
    printf("\n%d",*ptr[0]); //Prints zero. If *ptr[1] is given then *(ptr + 1) i.e ptr[1] is considered which is uninitialized one.
    
    return 0;
    }
    

提交回复
热议问题