Is there a reason why dynamic arrangement should not be declared like this?

Asked 1 years ago, Updated 1 years ago, 249 views

The size of the array is determined at compile time, so to declare an array of dynamic sizes

int size;
scanf("%d", &size);
int* arr = (int*)malloc(sizeof(int) * size);

I was taught to do this, but even if I code it as below, it goes back without a problem. (c, cpp )

int size;
scanf("%d",&size);
int arr[size];

I would like to know the reason why the program works without a problem even though the size of the array is not determined during compilation, and if that is the case, there is no problem in coding as below without using dynamic allocation such as malloc, and if there is any difference between c and cpp.

array declare c c++ initialization

2023-01-10 10:48

1 Answers

https://wikidocs.net/27184

C also has a standard, which depends on which standard you use.

This grammar is called a variable length array (VLA), which is possible in C99 and not before that.


2023-01-10 22:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.