0

I actually have an embarrassingly simple problem that I can't seem to overcome, currently. I'm trying to create a struct and then assign values to it in a separate function. Unfortunately, every solution I've tried doesn't seem to work and I have a feeling, I'm missing something really basic . . .

Here is the database struct (I've removed a few elements that aren't necessary here, so please ignore the simplicity. We actually read from a database but I'm creating a basic application for test purposes that is going to simulate a database that I'll be reading from to test a function):

struct Database
{
    double Output[12];
};

Database tempdatabase;

I have tried the following:

void InitVars()
{
    tempdatabase.Output[12] = {-1.938,29.063,0,0,0,0,0,0,0,0,0,0};
}

and

void InitVars()
{
    double temparray1[12] = {-1.938,29.063,0,0,0,0,0,0,0,0,0,0};
    tempdatabase.Output[12] = temparray1[12];
}

and

void InitVars()
{
    tempdatabase.Output[12] = new[]{-1.938,29.063,0,0,0,0,0,0,0,0,0,0};
}

I'm sure the answer to this is really, really simple but I haven't been able to have any success getting around this. Can anyone help me get this sorted out? Any help would be greatly appreciated. Thanks!

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
J B
  • 11
  • 1
  • You're not using the proper idioms. Create constructors for objects, or use aggregate initialization. – StoryTeller - Unslander Monica Mar 27 '17 at 18:52
  • when you assign a value to `tempdatabase.Output[12]`, it doesn't mean assigning a value to an array of 12 elements, but rather assigning a value to the 13th element of the array (which is out of bounds, and also is a double so it doesn't support initialiser lists) – Bettorun Mar 27 '17 at 18:53
  • Heh, sorry guys, I don't understand what you're saying and I learn more from examples (which I've spent a lot of time googling as of now anyway and can't seem to find a 1:1 solution). The following is what I ended up doing: void InitVars() { double temparray1[12] = {-1.938,29.063,0,0,0,0,0,0,0,0,0,0}; for (int arraysize = 0; arraysize < 12; arraysize++) { tempdatabase.Output[arraysize] = temparray1[arraysize]; } } Not sure if that's the best solution but I can't seem to translate your tips to actual solutions – J B Mar 27 '17 at 19:08
  • @JB See the linked duplicate. Use [`std::copy`](http://en.cppreference.com/w/cpp/algorithm/copy). – cdhowie Mar 27 '17 at 19:37

0 Answers0