assigning values to structure members

Discussion in 'C Programming' started by Pushkar Pradhan, Nov 16, 2003.

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?

2. Sheldon SimmsGuest

On Sun, 16 Nov 2003 00:12:05 -0600, Pushkar Pradhan wrote:

> 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.

Sheldon Simms, Nov 16, 2003

3. Richard HeathfieldGuest

Sheldon Simms wrote:

> On Sun, 16 Nov 2003 00:12:05 -0600, Pushkar Pradhan wrote:
>
>> 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 <not foo>).

> What you have done is fine.

Agreed.

--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Richard Heathfield, Nov 16, 2003
4. Robert StankowicGuest

"Pushkar Pradhan" <> schrieb im Newsbeitrag
news:...
> 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

Robert Stankowic, Nov 16, 2003
5. CBFalconerGuest

Richard Heathfield wrote:
> Sheldon Simms wrote:
> > On Sun, 16 Nov 2003 00:12:05 -0600, Pushkar Pradhan wrote:
> >
> >> 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 <not foo>).
>
> > 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.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.

CBFalconer, Nov 16, 2003

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:
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 Simms wrote:
> On Sun, 16 Nov 2003 00:12:05 -0600, Pushkar Pradhan wrote:
>
>
>>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.
>
>

7. Joona I PalasteGuest

Pushkar Pradhan <> scribbled the following:
> 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:
> 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.

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++ looks like line noise."
- Fred L. Baube III

Joona I Palaste, Nov 16, 2003