Structs

M

MatthewL

Hi,
I'm having a few problems using a strucutre.

struct iCoords

{

int X;

int Y;

};

iCoords myCoords[5] = {-1, -1};

myCoords[0].X = 325;

myCoords[0].Y = 240;

myCoords[1] = {225, 340};

I can initialize the struct fine with the first line of code, but there
after it complains.

For the second and third lines where I try to initialize the two X and Y the
compiler tells me I'm missing a ';' before '.'.

One the fouth line the compiler tells me I have too many initializers.



Can anyone explain what I'm doing wrong here please? :(
 
V

Victor Bazarov

MatthewL said:
Hi,
I'm having a few problems using a strucutre.

struct iCoords

{

int X;

int Y;

};

iCoords myCoords[5] = {-1, -1};

myCoords[0].X = 325;

myCoords[0].Y = 240;

myCoords[1] = {225, 340};

I can initialize the struct fine with the first line of code, but there
after it complains.

The line above is not initialisation, it's assignment. On the right
side of the assignment it expects an expression. What you wrote there
is not an expression, it's an aggregate initialiser, which is not
allowed where expressions are expected.
For the second and third lines where I try to initialize the two X and Y the
compiler tells me I'm missing a ';' before '.'.

One the fouth line the compiler tells me I have too many initializers.



Can anyone explain what I'm doing wrong here please? :(

Besides attempting to use an aggregate initialiser as an expression, you
seem (just seem) to be using assignments (executable statements) outside
of any function. It is not allowed.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,415
Latest member
SharonCran

Latest Threads

Top