473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

assigning values to structure members

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

Nov 13 '05 #1
6 7798
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.
Nov 13 '05 #2
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 : bi****@eton.powernet.co.uk
"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
Nov 13 '05 #3

"Pushkar Pradhan" <pu*****@gri.msstate.edu> schrieb im Newsbeitrag
news:3F**************@gri.msstate.edu...
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
Nov 13 '05 #4
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #5
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 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.


Nov 13 '05 #6
Pushkar Pradhan <pu*****@gri.msstate.edu> 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:
/* 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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++ looks like line noise."
- Fred L. Baube III
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: steveneng | last post by:
C++ Primer Plus Programming Exercises 4th Ed - Prate Help I'm trying to refresh myself and I'm stuck on this problem (not homework/school related but for personal advancement). 6: Do...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
7
by: cFleury | last post by:
I have an ASP.NET web form that is retaining values over different stations on the network, how can I stop this behavior ?. I didn’t do anything special to cause it. Thanks C.Fleury
9
by: Thiru .Net | last post by:
how to assign unsigned values in vb.net i have a statement like this in c# public const uint FEEDER = 0x00000001; when i tried to convert above into vb.net i said Public Const FEEDER As...
4
by: Jon | last post by:
This seems strange, but maybe there's some basic concept I'm missing. When I assign one class member to another, any methods that are applied to one are applied to both variables.I can't get the...
2
by: eBob.com | last post by:
I know that this must be a really dumb question but I just can't find an answer. I want to associate some information with a RichTextBox. The Tag property seems to be the intended way to "hang"...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
2
by: ranga044 | last post by:
I have declared an char array in my structure.After creating instance of the structure,i initialized its members in following way. structure student { char name; int marks; } int main() {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.