454,486 Members | 1,694 Online
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

# re: struct

 P: n/a (having trouble getting my reply through - hopefully, third time is a charm) On November 11, 2008 19:53, in comp.lang.c, BIll Cunningham (no****@nspam.invalid) wrote: > "Lew Pitcher" "The right brace that terminates the list of members may be followed by alist of variables, just as for any basic type. That is struct { . . . } x, y, z;is syntactically analogous to int x, y, z; Why an int? Why not a double? Why an int? Because K&R chose to use an int. Why not a double? Because K&R chose to use an int. Note the phrase "syntactically analogous". It means that the /syntax/ of struct { ... } x, y, z; is analogous to the /syntax/ of int x, y, z; where struct { ... } and int both represent storage types, and x, y, z; represents variables of that storage type. >in the sense that each statement declares x, y, and z to be variables ofthenamed type, and causes space to be allocated for them" (K&R1 p120 Chapter6Structures, Section 6.1 Basics)In other words, your struct point { int x; int y; };only defines a type of storage (named "point"), but does not allocate anyvariables of that type.However, struct point { int x; int y; } x, y, z;defines a type of storage (still named "point"), and allocates variablesx,y, and z as that type of storage. Note that the variables x and y are/not/the same as the structure elements x and y. x refers to a struct point called x, y refers to a struct point called y, x.x refers to an int called x /within/ the struct point called x x.y refers to an int called y /within/ the struct point called x y.x refers to an int called x /within/ the struct point called y, and y.y refers to an int called y /within/ the struct point called y I think perhaps leaving the x,y and z between } and ; off might be simplier. Simpler than what? To what purpose? -- Lew Pitcher Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request ---------- Slackware - Because I know what I'm doing. ------ Nov 12 '08 #1
4 Replies

 P: n/a "Lew Pitcher" >"Lew Pitcher" >"The right brace that terminates the list of members may be followed byalist of variables, just as for any basic type. That is struct { . . . } x, y, z;is syntactically analogous to int x, y, z; Why an int? Why not a double? Why an int? Because K&R chose to use an int. Why not a double? Because K&R chose to use an int. Note the phrase "syntactically analogous". It means that the /syntax/ of struct { ... } x, y, z; is analogous to the /syntax/ of int x, y, z; where struct { ... } and int both represent storage types, and x, y, z; represents variables of that storage type. [snip] Ok what about this example. struct node { int count; double store; node * pleftchild; node * prightchild; }c,s; Now we have an int and a double member. What are the c and the s types ? Bill Nov 12 '08 #2

 P: n/a "BIll Cunningham" Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 12 '08 #3

 P: n/a "Keith Thompson" Ok what about this example.struct node { int count; double store; node * pleftchild; node * prightchild;}c,s;Now we have an int and a double member. What are the c and the s types ? c and s are of type struct node, of course. Ok Keith I might have this. So c and s are now not only declared but defined too now. So this would be the same as, struct node c; struct node s; ? Bill Nov 12 '08 #4

 P: n/a "Bill Cunningham" "BIll Cunningham" >Ok what about this example.struct node { int count; double store; node * pleftchild; node * prightchild;}c,s;Now we have an int and a double member. What are the c and the s types ? c and s are of type struct node, of course. Ok Keith I might have this. So c and s are now not only declared but defined too now. So this would be the same as, struct node c; struct node s; Right. To be precise, this: struct node { int count; double store; node *pleftchild; node *prightchild; } c, s; is equivalent to this: struct node { int count; double store; node *pleftchild; node *prightchild; }; struct node c; struct node s; Personally, I prefer the latter. Though you *can* declare the type and a couple of objects of the type in a single declaration, I find it clearer to use one declaration for the type and one for each object. You could also replace struct node c; struct node s; by struct node c, s; but again, it's IMHO better style to declare one thing per declaration. -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 12 '08 #5

### This discussion thread is closed

Replies have been disabled for this discussion.