By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
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" <lp******@teksavvy.comwrote in message
news:b4*************************@TEKSAVVY.COM-Free...
>"The right brace that terminates the list of members may be followed by a
list 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 of
the
named type, and causes space to be allocated for them" (K&R1 p120 Chapter
6
Structures, 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 any
variables of that type.

However,
struct point {
int x;
int y;
} x, y, z;

defines a type of storage (still named "point"), and allocates variables
x,
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
Share this Question
Share on Google+
4 Replies


P: n/a

"Lew Pitcher" <lp******@teksavvy.comwrote in message
news:5f***************************@TEKSAVVY.COM-Free...
(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" <lp******@teksavvy.comwrote in message
news:b4*************************@TEKSAVVY.COM-Free...
>>"The right brace that terminates the list of members may be followed by
a
list 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" <no****@nspam.invalidwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
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" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"BIll Cunningham" <no****@nspam.invalidwrites:
[...]
>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" <no****@nspam.invalidwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>"BIll Cunningham" <no****@nspam.invalidwrites:
[...]
>>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 <http://www.ghoti.net/~kst>
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.