Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did the language designers of C do type equivalance like this?

I'm learning C and I'm reading about type equivalence.

I'm curious, does anyone have an opinion why they used structural equivalence for arrays and pointers but they used declaration equivalence for structs and unions?

Why the disparity there? What is the benefit of doing declaration equivalence with structs/unions and structural equivalence for everything else?

like image 996
KingNestor Avatar asked Dec 03 '22 08:12

KingNestor


2 Answers

I'm sure others will present C specific information, but I'll mention that type-equivalence is one of the classic main problems in programming languages theory. Determining whether two types are actually equivalent is a much trickier problem than it may seem.

For example, here are some overview slides from an academic course just to give you a taste of the headache.

like image 120
Uri Avatar answered Dec 04 '22 21:12

Uri


I'm sure it was because it looked like a good idea at the time.

Don't try to overthink the design of C. Kernighan & Ritchie were designing a system implementation language that might be good for other things, and wound up with decisions they regretted later (operator precedence being the best documented). Some of the issues were cleaned up by the Standards Committee, and others were too deeply ingrained.

As Uri points out in his answer, type equivalence is a difficult problem, and is therefore one of those likely to have been punted by K&R in a desire to get a working compiler soon rather than a clean language design later.

like image 45
David Thornley Avatar answered Dec 04 '22 20:12

David Thornley