更新时间:2022-06-22 22:26:19
编辑:刚注意到您正在使用C ++。在这种情况下,std :: vector绝对是最方便的解决方案。如果您对如何使用C风格的普通数组感到好奇,请继续阅读...
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...
您可以不要这样您需要做的是创建一个3维数组(或2维数组,具体取决于您的观点...)
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...)
在C的情况下,内存分配很麻烦...您可以静态分配:
As is always the case with C, memory allocation is a pain... You can statically allocate:
long double x[N][size][size];
(N是在编译时确定的。请确保它足够大。
也如果N确实很大,则可以声明数组(可以作为全局变量,也可以使用 static
关键字)。否则,可能会导致堆栈溢出。)
(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 static
keyword). If you don't you can get a stack-overflow.)
或者您可以使用动态内存分配:
Or you can use dynamic memory allocation:
long double (*x)[size][size]; // x is a pointer to size by size arrays
x = malloc(n * sizeof(*x));
//You can now use x[j][a][b]...
也许使用typedef看起来会更好(即使我不是100%肯定在上一个示例中也得到了数组声明...):
Maybe things look better with a typedef (even I am not 100% sure I got that array ceclaration right in the last example...):
typedef long double typename_here[size][size];
typename_here *x;
x = malloc(n * sizeof(typename_here);
顺便说一句,您当前的代码不起作用的原因是
BTW, the reason your current code doesn't work is
x(j)
语法是虚构的:)x(j)
syntax is ficticious :)