Is it possible to declare a variable when initializing an array?
For example:
#include <stdio.h>
main()
{
int x;
int arr[x];
scanf("%i", &x);
}
Or perhaps something similar?
Any help would be appreciated.
In general you may write
int x;
scanf("%i", &x);
int arr[x];
In this case the array arr will be a variable size array. The value of x shall be greater than 0.
Otherwise in this case
int x;
int arr[x];
the variable x is uninitialized (because it is a variable with automatic storage duration) and as a result the array declaration invokes undefined behavior.
xneeds to have a value before it's used to sizearr.arrdefinition it does not know about the future value ofx.scanfbetween the two variables.