开发者

Why isn't the compiler flashing any error?

开发者 https://www.devze.com 2023-01-29 23:47 出处:网络
This is my code: int a1[][3]={{1,2,3,4,5,6},{4,5,6,5}}; int (*q)[3]; q=a1; q is a pointer to an array of 3 integers. But a1 does not comply with 开发者_如何学JAVAq\'s type. Yet the assignment wor

This is my code:

int a1[][3]={{1,2,3,4,5,6},{4,5,6,5}};

int (*q)[3];

q=a1;

q is a pointer to an array of 3 integers. But a1 does not comply with 开发者_如何学JAVAq's type. Yet the assignment works and no error comes.

Can anyone explain why?


The types do comply. a1 is an array of length-3 arrays of ints. q is a pointer to a length-3 array of ints. An array decays to a pointer in most circumstances; this is one of them, so everything's fine!


See the C faq on arrays and pointers. Specifically, Question 6.2.


The types are equvalent - when you use a1 in the assignment statement it turns into a pointer, and presto - matching types.

Lots more information:

http://c-faq.com/aryptr/index.html


You are assigning the address of the first element of a1 to the pointer q.

0

精彩评论

暂无评论...
验证码 换一张
取 消