I try to create series of multidimensional arrays with for loops. User should input the number of arrays to be initialized. I want something like this:
int i;
printf("Enter the number of arrays you want: ");
scanf("%d",i)
for(int j=0;j<i;j++){
long double x(j)[size][size];
}
if I input 5, I want to have x0, x1, x2, x3 and x4.
Is it possible at all??
Thank you for any help.
Best Regards,
Emre.
Edit: Just noticed you are using C++. In this case, std::vector is definitely the most convenient solution. If you are curious about how to do this with plain arrays, C style, then read on…
You can’t do it like this. What you need to do is create a 3 dimensional array (or an array of 2-dimensional arrays, depending on your point of view…)
As is always the case with C, memory allocation is a pain… You can statically allocate:
(N is determined at compile time. Make sure you have it large enough.
Also if N is really big, declare the array stactically (either as a global variable or with the
statickeyword). If you don’t you can get a stack-overflow.)Or you can use dynamic memory allocation:
Maybe things look better with a typedef (even I am not 100% sure I got that array ceclaration right in the last example…):
BTW, the reason your current code doesn’t work is
x(j)syntax is ficticious 🙂