mercredi 31 juillet 2019

Filling a multidimensional array with random bytes in one line in C

I want to initialize a 2D matrix by filling it with random numbers (doubles).

So, for example, this code

#define N 1000

int main(void){
  double A[N];

  arc4random_buf(A,N*sizeof(double));

  return 0;
}

runs and produces a 1D array filled with random numbers as expected.

However, this

#define N 1000

int main(void){
  double A[N][N];

  arc4random_buf(A,N*N*sizeof(double));

  return 0;
}

produces a segmentation fault. I've tried initializing A with zeroes on the previous line, to no avail.

Is the only solution to use a loop here?




Aucun commentaire:

Enregistrer un commentaire