4

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];
highlevelcoder
  • 345
  • 1
  • 3
  • 10
  • 11
    I think most people despise Schildt's books… – hbw Jun 15 '10 at 08:33
  • 3
    Yup, and this is one reason. You must index the array name, not the structure name. He has the code right but the exmplanation wrong. In the example, `invtry` is the array name and `type` the structure name. Accorsding to his own logic, the code should be `cout << type[2].on_hand`. – MSalters Jun 15 '10 at 08:43
  • 2
    If you ask me, defining the type and declaring the array at the same type is a "don't" in the first place. – Daniel Daranas Jun 15 '10 at 08:44
  • 1
    @MSalters: So is it some kind of mistake in writing or? – highlevelcoder Jun 15 '10 at 08:58
  • 10
    Google `bullschildt` (and then burn your book). – fredoverflow Jun 15 '10 at 09:24
  • 7
    @learningtolive: Get yourself a [_decent C++ book_](http://stackoverflow.com/questions/388242/) immediately. The value of Schildt's books is measured in watt and is equivalent to the heat it produces when burnt minus the energy wasted in producing it. (This is always a negative value.) – sbi Jun 15 '10 at 09:26
  • 1
    WhaT about c++ from the ground up, maybe he learnt on mistakes :D – highlevelcoder Jun 15 '10 at 09:28
  • 1
    @FredOverflow: Ah, I didn't know that term. A nice one. `:)` – sbi Jun 15 '10 at 09:28
  • 2
    @learningtolive: Maybe he can. I have yet to hear of a good book from him, though. OTOH, there are a few other very good beginner C++ books. Look at the link in my previous comment. – sbi Jun 15 '10 at 09:28
  • 2
    What about amazon reviewers, it was my primary reference?? – highlevelcoder Jun 15 '10 at 09:29
  • 2
    @learningtolive: Amazon reviews can be (and often are) faked. – sbi Jun 15 '10 at 09:31
  • 1
    What a crap, now it seems I'm wasting my time all the time :) – highlevelcoder Jun 15 '10 at 09:33
  • 1
    @learningtolive: Not if you learn from your mistakes with the help of the real feedback you get here. You have a long way to go if you want to write C++ software, though - be patient, methodic and constant. – Daniel Daranas Jun 15 '10 at 09:35
  • 4
    @learningtolive: When it comes to learning C++, you cannot trust the opinions of beginners about books. By definition, beginners cannot possibly know if what they are taught is idiomatic C++ or not. Positive reviews from beginners probably mean the material is well *presented* or something, but that says nothing about the quality of the *contents*. – fredoverflow Jun 15 '10 at 09:37
  • 3
    @learningtolive: If you want good book reviews, the [ACCU has them](http://accu.org/index.php?module=bookreviews&func=search). – sbi Jun 15 '10 at 09:44
  • 1
    Thx but I think I'll finish first with the one I started with. – highlevelcoder Jun 15 '10 at 09:47
  • 5
    You have already wasted money. Don't make a second mistake by wasting time ;) – fredoverflow Jun 15 '10 at 09:49
  • 1
    What about Ivor Horton's Visual C++ 2010? – highlevelcoder Jun 15 '10 at 10:08
  • 3
    @learningtolive: You have been directed to http://stackoverflow.com/questions/388242/. Don't ignore that advice. – Daniel Daranas Jun 15 '10 at 10:15
  • 2
    @learningolive I have a rather simple rule for selecting C++ books - If it's not published by Addison Wesley, I thing twice and even three times before buying it. –  Jun 15 '10 at 10:17
  • 1
    I won't ignore it, just asking for the newest with VS2010 – highlevelcoder Jun 15 '10 at 10:20
  • 1
    @Daniel: Is MSalters also right? – highlevelcoder Jun 15 '10 at 10:35
  • 1
    @learningtolive: I am not the judge of whether everyone else in SO is right or wrong. About the comment you refer to, I think it is correct and I upvoted it. But that's just me. – Daniel Daranas Jun 15 '10 at 11:12

3 Answers3

4

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
  • 22,454
  • 9
  • 63
  • 116
  • So is it like that struct type{ char item[40];//0 double cost;//1 double retail;//2 int on_hand;//3 int lead_time;//4 }invtry[SIZE]; – highlevelcoder Jun 15 '10 at 09:01
  • @learningtolive No, this is nonsense. Please read the second part of my answer. – Daniel Daranas Jun 15 '10 at 09:09
  • How would I access lead_time member - by what index? inv[t].lead_time – highlevelcoder Jun 15 '10 at 09:23
  • 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 Jun 15 '10 at 09:31
  • This code is btw from the Schildts book: C++ from the ground up, starting on page 219 – highlevelcoder Jun 15 '10 at 09:36
  • No, It was the excuse for great coding style :) – highlevelcoder Jun 15 '10 at 09:45
  • `char item[40]` -> Fixed-size character arrays?!? Please burn the book RIGHT NOW, it will only teach you bad manners! – fredoverflow Jun 15 '10 at 09:52
  • @FredOverflow: Worse, the book seems to be using a char[40] for what should most likely be an std::string. – Daniel Daranas Jun 15 '10 at 10:13
  • How does structure logically connect member variables: i.e. I want exact info for item[1]? – highlevelcoder Jun 15 '10 at 11:53
  • @learningtolive: That is an implementation detail. If you want item[1], just ask for it: "char some_char = invtry[2].item[1];". – Daniel Daranas Jun 15 '10 at 11:58
  • 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... – highlevelcoder Jun 15 '10 at 12:39
  • @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 Jun 15 '10 at 12:48
  • @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 Jun 15 '10 at 13:06
1

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
  • 6,064
  • 4
  • 31
  • 38
  • 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 Jun 15 '10 at 08:49
0

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
  • 3,791
  • 1
  • 18
  • 17
  • What about C++ Primer Plus, it was my 2nd reference? – highlevelcoder Jun 15 '10 at 09:44
  • @learning: I have only heard positive things about C++ Primer Plus! – fredoverflow Jun 15 '10 at 09:50
  • Stroustrup is not recommended for beginners from what I heard? – highlevelcoder Jun 15 '10 at 09:52
  • 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 Jun 15 '10 at 09:52
  • Isn't it better to write in an "old" style and then learn both C & C++ in the same time than just base myself on the C++ strictly? – highlevelcoder Jun 15 '10 at 09:55
  • @learningtolive There are two C++ books by Stroustrup, "Programing P&P" is a learners book and fairly new, "The C++ Programming Language" is more of a reference book, which I wouldn't use for learning. – Fabio Fracassi Jun 15 '10 at 09:57
  • @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 Jun 15 '10 at 10:00
  • 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 Jun 15 '10 at 10:05
  • 1
    @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 Jun 15 '10 at 11:59
  • Nope, he said in the beginning chapter that if I learn C++ I'll also learn c in parallel. – highlevelcoder Jun 15 '10 at 14:23