What int (*ptr)[4] really means and how is it different than *ptr?

后端 未结 7 1667
暖寄归人
暖寄归人 2020-12-07 02:49
int (*p)[4] , *ptr;
int a[4] = {10,20,30,40};
printf(\"%p\\n%p\\n%p\",&a,a,&a[0]);
p = &a ;
//p=a;        gives error

//ptr = &a;   gives error
 ptr         


        
7条回答
  •  感动是毒
    2020-12-07 03:27

    p is a pointer to values of type int[4], i.e. a pointer to arrays of 4 integers each. Note that sizeof(*p) is 4 times sizeof(int).

    Now,

    • p = a fails because a decays to a pointer-to-int when assigned, while p points to a different type. Read more about decaying: What is array to pointer decay?
    • ptr = &a fails because ptr is actually a pointer-to-int; it doesn't have the same type as p. Declaring multiple variables on the same line is often confusing, because not all syntax applies to everything you declare; better split up those definitions to separate lines.

提交回复
热议问题