assigning values to structure members

P

Pushkar Pradhan

I have a struct like this:
struct point {
int x;
int y;
};

Then I need to assign a certain point "=" another pt.
eg.
struct point p1, pArr[10];

/* initialize all pts. */
Now assign p1 to a certain element of pArr, this is how I did and it
works fine.
p1 = pArr[someIndex];

However, I just read my C book again and it says this stuff must be done
like this actually:
p1.x = parr[someIndex].x;
p1.y = parr[someIndex].y;

Can anybody tell if I should continue with the former code or should I
make changes?
Pushkar Pradhan
 
S

Sheldon Simms

I have a struct like this:
struct point {
int x;
int y;
};

Then I need to assign a certain point "=" another pt.
eg.
struct point p1, pArr[10];

/* initialize all pts. */
Now assign p1 to a certain element of pArr, this is how I did and it
works fine.
p1 = pArr[someIndex];

However, I just read my C book again and it says this stuff must be done
like this actually:
p1.x = parr[someIndex].x;
p1.y = parr[someIndex].y;

Please tell us the name of the C book so that we can all avoid it.
What you have done is fine.
 
R

Richard Heathfield

Sheldon said:
I have a struct like this:
struct point {
int x;
int y;
};

Then I need to assign a certain point "=" another pt.
eg.
struct point p1, pArr[10];

/* initialize all pts. */
Now assign p1 to a certain element of pArr, this is how I did and it
works fine.
p1 = pArr[someIndex];

However, I just read my C book again and it says this stuff must be done
like this actually:
p1.x = parr[someIndex].x;
p1.y = parr[someIndex].y;

Please tell us the name of the C book so that we can all avoid it.

Careful. Maybe he has misread the book. (This has happened before in clc -
someone has said "my book says said:
What you have done is fine.

Agreed.
 
R

Robert Stankowic

Pushkar Pradhan said:
I have a struct like this:
struct point {
int x;
int y;
};

Then I need to assign a certain point "=" another pt.
eg.
struct point p1, pArr[10];

/* initialize all pts. */
Now assign p1 to a certain element of pArr, this is how I did and it
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
works fine.
p1 = pArr[someIndex];


No :)
pArr[someIndex] = p1;
at least your text says so (if my english did not get into the way :).
However, I just read my C book again and it says this stuff must be done
like this actually:
p1.x = parr[someIndex].x;
p1.y = parr[someIndex].y;

Can anybody tell if I should continue with the former code or should I
make changes?

The assignment is fine (in both directions)

Robert
 
C

CBFalconer

Richard said:
Sheldon said:
I have a struct like this:
struct point {
int x;
int y;
};

Then I need to assign a certain point "=" another pt.
eg.
struct point p1, pArr[10];

/* initialize all pts. */
Now assign p1 to a certain element of pArr, this is how I did
and it works fine.
p1 = pArr[someIndex];

However, I just read my C book again and it says this stuff
must be done like this actually:
p1.x = parr[someIndex].x;
p1.y = parr[someIndex].y;

Please tell us the name of the C book so that we can all
avoid it.

Careful. Maybe he has misread the book. (This has happened
before in clc - someone has said "my book says <foo>" when in
fact it says said:
What you have done is fine.

Agreed.

He MAY have misread a section to do with detecting equality.
Statements such as:

if (p1 == pArr[ix]) {/* whatever */}

are not defined, and must be broken up into tests on the
individual components. Again, the name of the book would be
worthwhile.
 
P

Pushkar Pradhan

The book is:
The C Programming Language by KERNIGHAN & RICHIE.
Ok I don't think they explicitly said that, I just concluded it from
this code in the book:
/* addpoint: add two points */
struct point addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
My question is couldn't you just do:
p1 += p2;



Sheldon said:
I have a struct like this:
struct point {
int x;
int y;
};

Then I need to assign a certain point "=" another pt.
eg.
struct point p1, pArr[10];

/* initialize all pts. */
Now assign p1 to a certain element of pArr, this is how I did and it
works fine.
p1 = pArr[someIndex];

However, I just read my C book again and it says this stuff must be done
like this actually:
p1.x = parr[someIndex].x;
p1.y = parr[someIndex].y;


Please tell us the name of the C book so that we can all avoid it.
What you have done is fine.
 
J

Joona I Palaste

Pushkar Pradhan said:
The book is:
The C Programming Language by KERNIGHAN & RICHIE.
Ok I don't think they explicitly said that, I just concluded it from
this code in the book:
/* addpoint: add two points */
struct point addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
My question is couldn't you just do:
p1 += p2;

This is because += is different than =. Simple assignment (with =) is
always defined for structures. "Plus assignment" (with +=) is not. For
example, if you had this structure:
struct namedpoint {
char *name;
int x;
int y;
};
how would the compiler treat "plus assignment" with those structures?

I hope this answers your question. If it doesn't, I'll provide more
detail, unless a real C guru does it first.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top