473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

re: struct

(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
4 1834

"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
"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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.