473,670 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7818
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.pow ernet.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.ms state.edu> schrieb im Newsbeitrag
news:3F******** ******@gri.msst ate.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********@yah oo.com) (cb********@wor ldnet.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.ms state.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.hel sinki.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
2576
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 programming exercise 3, but, instead of declaring an array of three CandyBar structures, use new to allocate the array dynamically. That's the question in case you guys don't ahve the book. I have Exercises 3 done and I can properly use structures. But...
14
74843
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; myarray=0; myarray=1; myarray=8;
13
7285
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( structure->bufferspace ) ). My question is, if I free just the structure, will the (e.g.) bufferspace be freed implicitly, or do I have to (as I currently am) free the members first? Thanks. -cjl
7
1250
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
4150
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 int64 = 0x00000001 but it gives error in the values assigned and saying
4
1252
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 code below to work (display sum, product and quotient) without re-initializing z1 each time. What's the problem here? Dim z0 As Complex = New Complex(CDbl(Me.TextBox1.Text), CDbl(Me.TextBox2.Text)) Dim z1 As Complex = New...
2
1363
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" some additional information on a Control. I've created a structure, rtbAuxInfo, for the information. I think that I have found the right syntax for assigning a pointer to the structure to the Tag property of the RichTextBox, but I can't figure...
10
4693
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 no-parameter constructor for my class with an empty function body. I then instantiated an object and tried printing its values, amazingly the members were already initialized. But how is this possible if I have not included any code for doing so. The...
2
2399
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
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8660
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7415
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1792
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.