如何在C中动态定义复数的三维数组,以便我可以访问[i] [j] [k]
符号,这对于访问数组很方便?
答案 0 :(得分:1)
您可以使用VLA,例如:
#include <stdio.h>
#include <complex.h>
int main(void) {
size_t n = 2,
m = 3,
o = 4;
double complex a[n][m][o];
a[1][2][3] = 1.0 + 0.5*I;
printf("%f + %fi\n", creal(a[1][2][3]), cimag(a[1][2][3]));
return 0;
}
答案 1 :(得分:1)
扩展 Bob__ &#39; example以在堆上而不是在堆栈上分配数组:
#include <stdlib.h>
#include <stdio.h>
#include <complex.h>
int main(void)
{
size_t n = 2, m = 3, o = 4;
double complex (*pa)[n][m][o] = malloc(sizeof *pa);
if (NULL == pa)
{
perror("malloc() failed");
exit(EXIT_FAILURE);
}
(*pa)[1][2][3] = 1.0 + 0.5*I;
printf("%f + %fi\n", creal((*pa)[1][2][3]), cimag((*pa)[1][2][3]));
free(pa);
return EXIT_SUCCESS;
}