473,795 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about Structures

Hi all it's me again with another question as I got further in my book. The
chapter I am in covers structres, abstract data and classes. I only read
through to the end of the coverage on structures. Trying to comprehend this
is harder than I thought it was going to be. I should of just skipped this
chapter and went right into pointers since they seem to be easier to use.
But anyways here i smy question:

you define a structure example:

struct phonerec
{
string fname;
string lname;
string number;
}phone[100];

If the above is right from what I understand phone will hold 100 fname,100
lname and 100 number?

to store data in in phone I would have to do somethinglike this if I
understood it all right.

phone[0].fname = "test value 1";
phone[0].lname = "test value 3";
phone[0].number = "123-456-4323"

Now from what I have read you can not use I/O streams to access structures,
only members of structures. so lets say that all 101 records are filled in
phone. If I wanted to output it all to a file I can do something like this:

outfile.open("d atafile.dat");
{
for (x=0; x<=100; x++)
outfile << phone[x].fname;
outfile <<" ";
outfile << phone[x].lname;
outfile <<" ";
outfile << phone[x].number;
outfile << endl;
}
Is there an easier way to output the data from the structure to the file?

I guess I should of just typed in my code into the compiler to actually see
if this works myself first and if it didn't figue out why it didn't but
ususally when I read something and go over it I get the point it's when I
try to do something I didn't read on that I have problems with it.

Now the next question I have for the more experienced programmers is with
the above data structure, what I have defined above the same as the
following below and if so how is one more efficient than the other:

string fname[100];
string lname[100];
string number[100];

//assume all 101 of each have data in them

outfile.open("d atafile.dat");
{
for (x=0; x<=100; x++)
outfile << fname[x];
outfile <<" ";
outfile << lname[x];
outfile <<" ";
outfile << number[x];
outfile << endl;
}

If all of the above is correct then I have understood the concept of how a
data structure works and is in the simple form, I did not yet read up on
structures within structures yet, this chapter is going to take me a little
longer than any of the other chapters in the book to grasp. I do truly wish
I could just say forget it and forget it even exists. Although structures,
Data Abstraction and classes were created for the purposes of making coding
easier but more complex than other alternatives.

Thank you for all replies to this message,
Shawn Mulligan

Jul 19 '05 #1
1 2735


kazack wrote:

Hi all it's me again with another question as I got further in my book. The
chapter I am in covers structres, abstract data and classes. I only read
through to the end of the coverage on structures. Trying to comprehend this
is harder than I thought it was going to be.
Actually the idea behind structures is very simple:
Things that belong together should stay together.

Example: A date.
A date always consists of a day, month and year. A day alone, a month
alone a year alone does not specify a date. Only the triplet of all of
them does.

Thus I group them together:

struct Date
{
int Day;
int Month;
int Year;
}

So whenever I talk about a 'Date', I mean the triplet of Day/Month/Year.
And whenever I pass I date around, I pass the whole triplet around.
I should of just skipped this
chapter and went right into pointers since they seem to be easier to use.
Suprises are waiting for for.
In fact, pointers are not much of use, unless you have structures.
But anyways here i smy question:

you define a structure example:

struct phonerec
{
string fname;
string lname;
string number;
}phone[100];

If the above is right from what I understand phone will hold 100 fname,100
lname and 100 number?
In principle you are right. Deep in the internals of memory there are 100
fname, 100 lname and 100 number. But you should change your thinking.

phone consists of 100 phonerecs.
Each phonerec consists of 1 fname, 1 lname and 1 number.

to store data in in phone I would have to do somethinglike this if I
understood it all right.

phone[0].fname = "test value 1";
phone[0].lname = "test value 3";
phone[0].number = "123-456-4323"
Yep. There it is again:

phone all phones (there are 100 of them, so which one?)
phone[0] aha, the phonerec with index 0. But a phonerec consists
of fname, lname and number, So which one?
phone[0].number Aha. The number field from the phonerec with index 0

Now from what I have read you can not use I/O streams to access structures,
only members of structures.
That's not entirely true, but it makes no real difference.
The thing is: A structure is a collection of fields. All of them form
the structure. The stream I/O routines on the other hand are designed
to work with basic data types, like int, double etc.. But a struct
isn't such a basic data type, it consists of them eventually, but it isn't.
so lets say that all 101 records are filled in
phone. If I wanted to output it all to a file I can do something like this:

outfile.open("d atafile.dat");
{
for (x=0; x<=100; x++)
outfile << phone[x].fname;
outfile <<" ";
outfile << phone[x].lname;
outfile <<" ";
outfile << phone[x].number;
outfile << endl;
}
Is there an easier way to output the data from the structure to the file?
No. That's the way you do it. You could write a function for outputing one
record and call that function in the loop. That would be a better organization
most of the time, but basically it's just the same thing: you output each
field individually (Who says that you need to output the whole structure?
What about formatting of the individual fields? What about seperating text?)

I guess I should of just typed in my code into the compiler to actually see
if this works myself first and if it didn't figue out why it didn't but
ususally when I read something and go over it I get the point it's when I
try to do something I didn't read on that I have problems with it.
Reading is just one thing. You actually need to practice programming.
Knowing about if, for, while and all the other keywords is not enough.
Beeing able to combine things is what programming is all about. It is
like plaing chess. Just knowing how the pieces can move is not enough
to make you a chess player.

Now the next question I have for the more experienced programmers is with
the above data structure, what I have defined above the same as the
following below and if so how is one more efficient than the other:

At your level just forget ybout efficiency right now. Learn to walk,
then learn to run, then learn to run fast, then try to win the NY marathon.
Not the other way round.
string fname[100];
string lname[100];
string number[100];

Those are 3 arrays. There is no connection between them.

//assume all 101 of each have data in them
They don't. An array

string fname[100]

has exactly 100 elements. Their valid indices are 0 .. 99
(count them, there are exactly 100 numbers in 0 .. 99)

outfile.open("d atafile.dat");
{
for (x=0; x<=100; x++)
outfile << fname[x];
outfile <<" ";
outfile << lname[x];
outfile <<" ";
outfile << number[x];
outfile << endl;
}

If all of the above is correct then I have understood the concept of how a
data structure works and is in the simple form, I did not yet read up on
structures within structures yet,
That's not a big deal:

// A Date consists of a Day, a Month, a Year
struct Date
{
int Day;
int Month;
int Year;
};

// A Name consists of a FirstName, a LastName
struct Name
{
string FirstName;
string LastName;
};

// a person is described by
// a Name (which contains a firstname and a lastname
// a Birthdate ( which consists of a day, a month, a year
struct Person
{
Name TheName;
Date BirthDate;
};
this chapter is going to take me a little
longer than any of the other chapters in the book to grasp. I do truly wish
I could just say forget it and forget it even exists.
Don't.
For one it's your entry point into a whole new world of dynamic data structures.
Also they make coding easier and simpler.
Also maintenance is easier.
Consider that you want to upgrade your program which uses aboves Person structure.
You want to add an address to the person. Simple just add a structure

struct Address
{
string Town;
string Street;
int Streetnumber;
}

and upgrade the person:

struct Person
{
Name TheName;
Date BirthDate;
Address TheAddress;
};

And throughout the whole program whenever you have a person object,
you also have it's address. If you wouldn't have used structures, you
would have needed to add the address to all the function calls and
parameter lists.
Although structures,
Data Abstraction and classes were created for the purposes of making coding
easier but more complex than other alternatives.


Not really. In the end everything becomes simpler. I don't need to worry any
longer to pass Day/Month/Year to a function. I just pass it a Date and know
that it receives everything needed to descrive a date.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #2

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

Similar topics

5
6381
by: achrist | last post by:
Here's a program: #include <stdio.h> int main(int argc, char **argv) { double an_array = {0.0, 1.0, 2.0, 3.0, 4.0}; typedef struct a_struct {double v0, v1, v2,v3, v4;} a_type; typedef a_type *a_type_ptr; a_type_ptr s_ptr;
11
3193
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example: int main void() { while there are more data types { print next data type; }
5
1911
by: Shwetabh | last post by:
Hi everyone. My question is, why are data structures implemented only with struct data type? Why not union when it is more efficient as compared with structures? Thanks in advance
4
7298
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
10
5263
by: jack | last post by:
Hi guys, I am working on a project which requires an implementation of discrete event simulation in C using linked lists. I would greatly appreciate if someone could provide with some sources on how to approach DES. Please help me out. Thanks, Jack
18
5712
by: Steven | last post by:
Hi I'm new to js so this prob is a simple question but I haven't been able to solve it. I have 2 text fields which let me pick a graphic file and want to display then in 2 image place holders so I want to call a function SetImg passing the id of the input file and the image name to to set the source to the file value fron the input field. Here is what I have done <head>
15
1858
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
0
1358
by: Art Cummings | last post by:
Good morning all. I just finished an assignment using structures. In this assignment I used an array of structures. What I would have liked was to use an array of structures with a function. The problem I ran into, was when the i tried to read the data in the structure I got garbage. Normally when I use arrays they retain the information that I enter once the function completes. I'm thinking that it has something to do with using the...
4
2090
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
0
9519
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
10213
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...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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
6780
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
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3
2920
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.