0

I'm writing an app in Visual Studio C++ and I have problem with assigning values to the elements of the array, which is array of elements of structure type. Compiler is reporting syntax error for the assigning part of the code. Is it possible in anyway to assign elements of array which are of structure type?

typedef struct {
    CString x;
    double y;
} Point;


Point p[3];
p[0] = {"first", 10.0};
p[1] = {"second", 20.0};
p[2] = {"third", 30.0};

4 Answers 4

5

Give your struct a constructor:

struct Point {
    CString x;
    double y;
   Point( const CString & s = "" , double ay = 0.0 ) : x(s), y(ay) {}
};

You can then say:

Point p[3];
p[0] = Point( "first", 10.0 );
Sign up to request clarification or add additional context in comments.

Comments

4

You can use an initializer when the array is being declared:

struct Point{
    CString x;
    double y;
};

Point p[3] = {
  {CString("first"), 10.0},
  {CString("second"), 20.0},
  {CString("third"), 30.0}
};

But not on assignment.

1 Comment

My bad. I made a mistake when testing it.
1

You can not set your data in this way. Instead write:

p[0].x = "first": p[0].y = 10.0;
...

Comments

0

What Neil says is right indeed!!

Some more info here...

Comments

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.