1

This is a fragment of my code:

typedef float point2[2];

point2 a = {-90, -90};
point2 b = {-90, 90};
point2 c = {90, 90};
point2 d = {90, -90};

glBegin(GL_POLYGON);
    glVertex2fv(a);
    glVertex2fv(b);
    glVertex2fv(c);
    glVertex2fv(d);
glEnd();

And this goes really well. But later when I try to write new values into these arrays, like:

a = {-66, -66};
b = {-66, 66};

And here I get an error:

error: assigning to an array from an initializer list

And I understand, that I can't assign values directly to an array after its declaration. But how this should looks like?

4
  • 2
    Initializer lists are just usable during initialization Commented Oct 17, 2015 at 13:07
  • I'm voting to close this question as off-topic because it is very poor and you can easily find it in any textbook!!! Commented Oct 17, 2015 at 13:17
  • @gsamaras Yes, I know. But like I said - I wanted to know if there is more convinient way then just a[0]=... a[1]=... Commented Oct 17, 2015 at 13:29
  • 1
    OK then @DzikiChrzan, I will retrieve my downvote and as a sorry I will upvote an answer of yours I like. Commented Oct 17, 2015 at 14:24

6 Answers 6

3

You can't assign to an array using an initializer list. But if you use std::array instead of a C array then this is possible:

#include <array>
typedef std::array<float, 2> point2;

point2 a = {-90.0, -90.0};
a = {-66, -66};

And you can still pass it to a function like glVertex2fv that takes a pointer to an array by using std::array::data:

glVertex2fv(a.data());

Live demo.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, so there IS a way to do it different! Thanks, this is answer I was looking for. ;)
2

In those statements, you are not assigning values to an array, you are initializing that array. Changing values in an array can be done like this:

a[0] = -66;
a[1] = -66;

Comments

2

You cant use initializer list once the array is initialized, just do:

a[0] = -66;
a[1] = -66;

8 Comments

first solution should work but it has unnecessary cost
@HumamHelfawi yes, I just mentioned it, because I assumed the OP already knows how to access the array elements but was looking for a denser notation.
Arrays in C++ are not objects. They don't have a constructor. And your first suggestion doesn't work, because you can't modify the pointer, to which your locally defined array variable is pointing, as well.
@AlgirdasPreidžius hm? The locally defined array variable is not pointing to a pointer
@AlgirdasPreidžius but you are right, that it doesnt work, because there is no constructor to call. My mistake, I will edit
|
2

When your case is really about points in a plain, you schould prefer to use a class instead of an array.

class point2 {
    private:
        float x_;
        float y_;
    public:
        point2(const float x, const float y): 
            x_{x}, y_{y} {};
        point2(const point2& o): 
            x_{o.x_}, y_{o.y_} {};

        operator = (const point2& o) {
            x_ = o.x_;
            y_ = o.y_;
        }
        // ...
};


point2 a = {90.0, 90.0}; // initialize a

a = {45.0, 45.0}; // untested, should work
a = point2{45.0, 45.0} // will work.

As you see, this will give you a more natural and expressive interface. Such interfaces always improve readability and maintaiability.

2 Comments

A great idea in general but perhaps not so convenient if you primarily want to pass the point to a third-party library that is expecting a pointer to an array. Which is the case here. You could have a point2 class that contains an array or std::array and provide convenience getters/setters for x and y.
Yes, in that case you can adapt the internal representation of course.
1

you should do something like:

a[0] =-66;
a[1]=-66;

Comments

1

This is possible during initialization only. Use:

a[0]= - 66;
a[1] = -66;

However you can create temporary array and then copy it.

point2 tmp = {-66, -66 }
System.arraycopy( tmp, 0, a, 0, src.length );

But it looks like an overkill for such a task.

1 Comment

Hm, okay. I know that I can do this in your way, but I was hoping that maybe there is more convenient way. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.