1

Please help me, I am quite new to C++

I code this in VS 2010

I have a global variable

int avg[SAMPLE][3]; 

I would like to modify the value in main program by doing something like this:

avg[SAMPLE][3] = {
{30,96,56}, {13,114,55}, {16,118,46},{19,143,64}, {12,129,68},{13,153,69},{15,120,67}

};

However, the VS shows an error: IntelliSense: expected an expression

But it is okay when I declare the value directly to global variable

int avg[SAMPLE][3] = {
{30,96,56}, {13,114,55}, {16,118,46},{19,143,64}, {12,129,68},{13,153,69},{15,120,67}

};

Perhaps this is trivial, but please help me, I am new into C++, and therefore, do not have a clue about it.

Thank you very much.

Raymond
  • 604
  • 1
  • 9
  • 27

3 Answers3

1

Raw (built-in) arrays are not assignable.

However, you can use std::array (fixed size) or std::vector, which are assignable.

It's not a good idea to use global variables. It is a good idea to reserve all uppercase for macro names.

In C++11 (which is not supported by Visual Studio 2010, but you can just get a new free version like Visual Studio 2013),

#include <array>
#include <iostream>
#include <vector>
using namespace std;

struct Sample { int x, y, z; };

auto main()
    -> int
{
    vector<Sample> avg =
    {
        {30,96,56}, {13,114,55}, {16,118,46},{19,143,64}, {12,129,68},{13,153,69},{15,120,67}
    };

    avg = { {1, 1, 1}, {2, 2, 2} };

    for( Sample const s : avg )
    {
        cout << s.x << ", " << s.y << ", " << s.z << endl;
    }
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Just of curiosity...why on earth do you prefer your main() function to be a lambda :)? Is that even guaranteed to work? I thought the compiler could mangle the names on lambdas... – TripShock Dec 31 '13 at 05:52
  • @TripShock: it's not a lambda. but it's nearly the same syntax. i use one common syntax for all functions, including function templates. that's much easier as i see it. this places the function name at a fixed visual place, and the function result type at fixed place, so that it's much easier to scan the code. it's also more clean (less variation) with just one syntax. – Cheers and hth. - Alf Dec 31 '13 at 05:57
1

The syntax you used is one that can be used to initialize an array. However, in C/C++, you cannot use that same syntax to assign multiple values to the array. You will have to resort to assigning each value, use a loop, etc.

Just searched a bit more and found a possible duplicate: c++ array assignment of multiple values

Community
  • 1
  • 1
TripShock
  • 4,081
  • 5
  • 30
  • 39
1

Re-assigning with an initializer list is not available in C++. C++11 adds this option to classes but not to static C arrays like your example.

What you can do is have one or more const global/static of the value you need and memcpy them when needed:

static const int g_avg[SAMPLE][3] = {
   {30,96,56}, {13,114,55}, {16,118,46},{19,143,64}, {12,129,68},{13,153,69},{15,120,67}
};

// usage
int avg[SAMPLE][3];
// ...
memcpy(avg, g_avg, sizeof(avg));
egur
  • 7,830
  • 2
  • 27
  • 47
  • You're welcome. Feel free to +1 on the answer as well :) – egur Jan 01 '14 at 13:33
  • Ùsing `memcpy` is bad advice, since (1) it's only well defined for standard layout item types, and (2) it's low level and easy to get wrong, a bug attractor. Continuing to use raw arrays as the default choice is also bad advice, but if one does that, then `std::copy` is efficient enough, generally applicable (as opposed to `memcpy`), and far more safe to use. This question is about **C++**, not about C. – Cheers and hth. - Alf Jan 01 '14 at 20:02