Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic initialization of a long array at compile or initialization time

Here is a code snippet illustrating my question:

const float D    = 0.1F;
const float A[4] = {sin(0*D), sin(1*D), sin(2*D), sin(3*D)};

Imagine that global array A is much longer and you don't want to do all of this repetitive typing. Is there a shorter way to initialize array A at compile or initialization time, i.e. without having to write initialization function and call it somewhere in my program?

like image 718
Paul Jurczak Avatar asked Aug 10 '14 20:08

Paul Jurczak


1 Answers

You could initialize A during dynamic initialization time as follows:

const float *init_a(float x_)
{
  static float data[4];
  for(unsigned i=0; i<4; ++i)
    data[i]=sin(i*x_);
  return data;
}

const float D=0.1f;
const float *A=init_a(D);
like image 71
JarkkoL Avatar answered Nov 15 '22 05:11

JarkkoL