1

Since it's not possible to assign values to a const after it's initialized, how can I achieve this?

const double input1[1000][2];
for(int i = 0; i < 1000; i++){
    input1[i][0] = i;
    input1[i][1] = i*2;
}
  • Why would you want to? Instead of using `input1`, user code can employ `i` instead of `input1[i][0]` and `i*2` instead of `input1[i][1]`. – Peter Feb 27 '18 at 00:40
  • If it const you need to initialise it using the during the compilation with the constant values. – 0___________ Feb 27 '18 at 00:51
  • 2
    You are declaring an array that is clearly not `const` as `const`, that makes no sense. If you want that your `const` array is initialized with values, the you have to do it the hard way `const double input1[1000][2] = { {0,1}, {0,2}, {0,3}, ... }`; – Pablo Feb 27 '18 at 01:01
  • If you use an appropriate build system, then you can generate the initializer with an script. If the data has to be `const`. Note that `const` is only a qualifier and you can always do something wrong with it, specially in [tag:c]. Of course `const` would protect you from accidentally doing something wrong, and I presume it would help the compiler optimize. But perhaps it's not worth it. – Iharob Al Asimi Feb 27 '18 at 01:18
  • It has the purpose of be the training data for a neural network exercise which calculates the result of a multiplication (I know it's useless, but it has learning purposes). The network is called by a function with a const double[][] parameter written by a third party library. I'm new with C, so I don't know if there's another way of doing this, for example, passing a non-const double[][]. Thank you for your reply. – David Dominguez Herrera Feb 27 '18 at 01:54

3 Answers3

4

You'll have to generate the explicit initialization (i.e. the { {0, 0}, {1, 2}, {2, 4}, ... etc etc ... {1000, 2000} }.

You can do this with map/apply macros, or with your build system - having it run some script which generates that initialization sequence and plants it into the file.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
1

You mentioned in the comments that this data is a training session data for a neural network.

I assume that you don't want to input 2000 values by hand and I assume that these values come from another file, perhaps a csv or the output of another program that generates the training data.

In that case, I'd write a script that generates a header file from the training data that looks like this:

// training_data.h
// trainng data, automatically generated by script gen_training_data.py
// do not modify this file

#ifndef TRAINING_DATA_H
#define TRAINING_DATA_H

const double traing_data[1000][2] = {
    { 0, 1 },
    { 0, 2 },
    ....
};

#endif

The script (that you can write with other languages, bash, python, perl) would take the training data and generate the header file for you, so you don't have to write 2000 values by hand. Then you can use it in your C program like this:

#include <stdio.h>
#include "training_data.h" // include auto generated file

int main(void)
{
    ...
    feed_to_neural_network(training_data);
    ...
}

If you use a build system like cmake, you can let cmake execute your script so that it autogenerates the header file before compiling. That would save you on the long run a lot of time.

Pablo
  • 13,271
  • 4
  • 39
  • 59
-1

I don't understand why would you want to store the index value in the first index. But removing const can solve your issue. But better thing to do would be, to just store the value instead of index. Like following:

double input1[1000];

for (int i = 0; i < 1000; i++)
{
    input1[i] = i * 2;
}
Billy
  • 183
  • 2
  • 14
  • Hi Chux, i understand that assignment can't be done to a const object and hence i suggested in above answer to remove const. And also if you see the code i've suggested, it says `double` and not `const double`. – Billy Feb 27 '18 at 04:11
  • Since, he was looking to give specific value to an index (twice of index), it did not seem feasible or efficient way to initialize them all one by one. Hence i suggested to remove const and run a loop for assignment. – Billy Feb 27 '18 at 04:13