c Declare an array of sizes specified by the language variable

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

int i;
scanf("%d", &i);
int aa[i];

I think I saw the code to declare the arrangement Why do I get an error?

array c

2022-09-20 10:26

1 Answers

The array must be sized at compile time, so it cannot be declared as intaa[variable]; and must be declared as intaa[constant]; type.

Therefore, it cannot be done like int aa[i]; and should be used using dynamic allocation like int *aa=malloc(sizeof(int)*i);. However, the i value must not be less than or equal to zero.

For your information, C99 also has a variable length array, but it is not used very well. https://en.wikipedia.org/wiki/Variable-length_array

2022-09-20 10:26

If you have any answers or tips

© 2022 pinfo. All rights reserved.