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
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.
© 2024 OneMinuteCode. All rights reserved.