Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the assignment-expression in array brackets in C?

In C89 A.1.2.2 Declarations

direct-declarator [ constant-expression ]

In C99 A.2.2 Declarations

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

direct-declarator [ type-qualifier-list_opt assignment-expression_opt ]

I understand assignment-expression_opt as var = 1;. But arrays are not declared like int arr[i = 0];. Why does C99 use the term "assignment-expression" instead of "constant-expression"and what does it mean?

>Solution :

In C89 there was no support for variable length arrays. This means that array sizes must be fixed at compile time.

Starting with C99, declarations and statements may be mixed within a block, so full expressions that must be executed are now allowed as an initializer. This also is what allows for variable length arrays to be created.

So a declaration like int arr[i=0]; is valid syntax, although it’s invalid because it created an array of size 0. int arr[i=2]; is valid and will create arr as an array of int of size 2, and it sets i to 2.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading