verito verito - 1 year ago 98
C Question

Complex numbers 3D array

How do I define three dimensional array of complex numbers dynamically in C so that I can access in

[i] [j] [k]
notations which is handy in accessing arrays?

alk alk
Answer Source

Extending Bob__'s example to have the array allocated on the heap instead of on the stack:

#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");

  (*pa)[1][2][3] = 1.0 + 0.5*I;

  printf("%f + %fi\n", creal((*pa)[1][2][3]), cimag((*pa)[1][2][3]));


  return EXIT_SUCCESS;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download