tags:

views:

155

answers:

3

Hi, I'm studying C++ from Schildt's book and don't quite understand what does he mean under third structure; Can somebody explain this ->

To access a specific structure within an array of structures, you must index the structure name. For example, to display the on_hand member of the third structure, you would write cout << invtry[2].on_hand;

Some code:

struct type{
 char item[40];
 double cost;
 double retail;
 int on_hand;
 int lead_time;
}invtry[SIZE];
+4  A: 

The third structure in an array of structures is the one placed in the third position in the array, i.e., the one with index 2.

In your (ugly) code, invtry is declared as an array (of size SIZE) of structures of type type. Hence invtry[0] is the first element, invtry[1] the second, and invtry[2] the third - assuming, of course, SIZE >= 3.


Normally, you would write:

struct type{
 char item[40];
 double cost;
 double retail;
 int on_hand;
 int lead_time;
};

const int SIZE = 500;

type invtry[SIZE];

This is synonymous to what you wrote, except for the definition of SIZE of course. But it leads to less confusion - in one part you say what a type (terrible name for the struct!) is - in other words, you define the type type. Later, you create an array of structs of type type, called invtry.

Doing this in the same line, as the author did, is simply awful - to my eyes.

Now you have an array of 500 structs. If "type" was "Product", you would have an array representing 500 products. Each one with its item, cost, retail, etc.

To access the third struct in the array, write invtry[2]. to access its particular on_hand field, write invtry[2].on_hand. This has nothing to do with the specific position of on_hand in the layout of the defined type.

If you want the lead_time of the third structure, first access the third structure and then its lead_time member: invtry[2].lead_time.

Of course since type does not have a default (parameterless) constructor, the 500 products are uninitialized - you have garbage in them. But that is your problem.

Daniel Daranas
So is it like thatstruct type{ char item[40];//0 double cost;//1 double retail;//2 int on_hand;//3 int lead_time;//4}invtry[SIZE];
@learningtolive No, this is nonsense. Please read the second part of my answer.
Daniel Daranas
How would I access lead_time member - by what index?inv[t].lead_time
You don't access struct members by indexes. You first access the struct, then any member that you want. I made a small edit to my answer to also include this case. Also, I suggest that you pick a good book on C++ basics. Several StackOverflow questions recommend books of this kind.
Daniel Daranas
This code is btw from the Schildts book: C++ from the ground up, starting on page 219
@learningtolive: I did not deny this fact.
Daniel Daranas
No, It was the excuse for great coding style :)
`char item[40]` -> Fixed-size character arrays?!? Please burn the book RIGHT NOW, it will only teach you bad manners!
FredOverflow
@FredOverflow: Worse, the book seems to be using a char[40] for what should most likely be an std::string.
Daniel Daranas
How does structure logically connect member variables: i.e. I want exact info for item[1]?
@learningtolive: That is an implementation detail. If you want item[1], just ask for it: "char some_char = invtry[2].item[1];".
Daniel Daranas
To finalize if I have array of structures of length 100; that means I'm handling with <=100 structures where each of these stores its own copy of structure members...In other words number of structures equals length of struct array...
@learningtolive: An array of 500 structures: "type invtry[SIZE]", contains 500 structures: from 0 to 499. In the same way as "int mycoolarray[500]" contains 500 integers, from 0 to 499. That is usually called the "size" of the array, rather than its length.
Daniel Daranas
So am I right ?
@learningtolive: You said "if I have array of structures of length 100; that means I'm handling with <=100 structures"... well, yes, you have "<=100" structures; more precisely, you have exactly 100 structures. And each of them has its own copy of (nonstatic) structure members.
Daniel Daranas
+1  A: 

Try substituting 'array item' for 'structure'.

So to access the 3rd item in the invtry array (which is an array of structs), you'd use invtry[2] (2 rather than 3 as the index is 0-based), followed by the member variable you wish to read...

i.e. invtry[2].on_hand gets the value held in 'on_hand' of the 3rd struct in the array 'invtry'

Dave
I'm betting the OP is confused by the fact C arrays are 0 based, voting you up. I do think your explanation could be a lot better though. :-)
Omnifarious
Thanks, I agree with you there! A little hurried...
Dave
+1  A: 

You shouldn't try to learn from a Schildt book. They are seriously flawed. Much information in it is seriously outdated or downright wrong. The code is written in a ugly and bad style with many C-isms in it. see ACCU Book reviews for a more detailed review of his books.

Try "Accelerated C++" by Koenig and Moo or "Programming Principles and Practice using C++" by Stroustrup for good beginners guides.

Fabio Fracassi
What about C++ Primer Plus, it was my 2nd reference?
@learning: I have only heard positive things about C++ Primer Plus!
FredOverflow
Stroustrup is not recommended for beginners from what I heard?
Don't know it myself, but the reviews (again by ACCU.org which is usualy quite reliable) are not that good either. Besides the 2nd edition is really old (current is 5th).The books I recommended are generally held in high esteem, because they teach you C++ with a modern Style from the beginning, and not C with added features, like many older books.
Fabio Fracassi
Fabio Fracassi
@FredOverflow Sure that you are not confusing it with the "C++ Primer" by Stanly Lippman? (Which is said to be good, too, but I haven't read it.)
Fabio Fracassi
There are different opinions on this one. IMVHO trying to learn both C++ and C at the same time is not a good Idea. The languages are similar syntax-wise, but the approach and mindset is quite different, and what is regarded good style in C, is often regarded as very bad C++.
Fabio Fracassi
Ok I'll keep that in mind, again it was Schildts quote.
@learning: Schildt recommends to learn C first? Another sign of his incompetence. The creator of C++ says that it's a [bad mistake](http://www2.research.att.com/~bs/bs_faq.html#prerequisite) to learn C as a stepping stone to C++.
FredOverflow
Nope, he said in the beginning chapter that if I learn C++ I'll also learn c in parallel.