473,396 Members | 1,666 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,396 software developers and data experts.

A clarification please

mdh
FAQ 1.7 shows that, amongst other things,

int i = 0;

is a definition.

Page 128 of K&R say:

"A struct declaration defines a type. 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;

in the sense that each statement declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. {My
emphasis}

My understanding was that a definition causes space to be set aside,
( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
in the above example. How can these two ideas be reconciled?

Thanks as usual.
Aug 18 '08 #1
13 1183
mdh wrote:
FAQ 1.7 shows that, amongst other things,

int i = 0;

is a definition.

Page 128 of K&R say:

"A struct declaration defines a type. 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;

in the sense that each statement declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. {My
emphasis}

My understanding was that a definition causes space to be set aside,
( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
in the above example. How can these two ideas be reconciled?
By noting that your concept of what a definition is isn't quite right.

A declaration that explicitly initializes the value of a variable is
always a definition. The same is true of a file scope declaration
using the keyword 'static', or any other declaration that does not use
the keyword 'extern'. That last case covers 'int i;' when it occurs at
block scope.

However, all other variable declarations are considered "tentative",
including "int i;" if it occurs at file scope, and that's where it
gets tricky. If a tentative definition is followed by a later
declaration of the same variable name in the same scope that includes
an initializer or uses the keyword 'static', the tentative definition
becomes a real definition. If, by the end of the translation unit,
there are no non-tentative definitions of a variable, then the
tentative definition becomes a real one, unless it was declared
'extern', in which case it becomes a declaration of a variable who's
actual definition lies elsewhere.

Aug 18 '08 #2
On Mon, 18 Aug 2008 15:23:06 -0700 (PDT), mdh <md**@comcast.net>
wrote:
>FAQ 1.7 shows that, amongst other things,

int i = 0;

is a definition.

Page 128 of K&R say:

"A struct declaration defines a type. 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;

in the sense that each statement declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. {My
emphasis}

My understanding was that a definition causes space to be set aside,
( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
in the above example. How can these two ideas be reconciled?
By correcting your understanding.

int i; is a definition. It causes space to be set aside (in some
implementation specific sense of the phrase) for object i. If it
didn't, code such as i = j+k; would have no place to store the result.

extern int i; is a declaration. It promises that i is in fact defined
somewhere else and the linker will be able to resolve its location.

struct t {...}; is a declaration. It doesn't define an object. It
does "define" a new type. (I prefer to say it declares the type but
it's hard to argue with K&R.) In any event, defining a type is
different than defining an object or function. struct t {...} x; is a
definition. It defines the object x and reserves space for it.

All of which proves that the word "define" and words derived from it
mean different things when used to describe the creation of objects or
used to describe other aspects of the language.

This is not that unusual. The word "token" means different things
when talking about the preprocessor, language syntax, or the use of
the standard function strtok. Context is important when trying to
decide what things mean.

--
Remove del for email
Aug 18 '08 #3
mdh
On Aug 18, 4:40*pm, jameskuy...@verizon.net wrote:
mdh wrote:
FAQ 1.7 shows that, amongst other things,
int i = 0;
is a definition.
Page 128 of K&R say:
"A struct declaration defines a type. 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;
in the sense that each statement declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. *{My
emphasis}
My understanding was that a definition causes space to be set aside,
( eg int i = 0) *but not a declaration ( eg int i) and the x, y, and z
in the above example. How can these two ideas be reconciled?

By noting that your concept of what a definition is isn't quite right.

A declaration that explicitly initializes the value of a variable is
always a definition. The same is true of a file scope declaration
using the keyword 'static', or any other declaration that does not use
the keyword 'extern'. That last case covers 'int i;' when it occurs at
block scope.

However, all other variable declarations are considered "tentative",
including "int i;" if it occurs at file scope, and that's where it
gets tricky. If a tentative definition is *followed by a later
declaration of the same variable name in the same scope that includes
an initializer or uses the keyword 'static', the tentative definition
becomes a real definition. If, by the end of the translation unit,
there are no non-tentative definitions of a variable, then the
tentative definition becomes a real one, unless it was declared
'extern', in which case it becomes a declaration of a variable who's
actual definition lies elsewhere.
My head is spinning!! :-)

Thank you for that explanation.
Aug 19 '08 #4
mdh
On Aug 18, 4:48*pm, Barry Schwarz <schwa...@dqel.comwrote:
On Mon, 18 Aug 2008 15:23:06 -0700 (PDT), mdh <m...@comcast.net>
wrote:
FAQ 1.7 shows that, amongst other things,
int i = 0;
is a definition.

struct {.....} *x, y, z:
t declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. *{My
emphasis}

How can these two ideas be reconciled?

By correcting your understanding.

That, sadly, has been happening a lot lately!! :-)
>
int i; is a definition. *It causes space to be set aside (in some
implementation specific sense of the phrase) for object i. *If it
didn't, code such as i = j+k; would have no place to store the result.

extern int i; is a declaration. *It promises that i is in fact defined
somewhere else and the linker will be able to resolve its location.

struct t {...}; is a declaration. *It doesn't define an object. *It
does "define" a new type. *(I prefer to say it declares the type but
it's hard to argue with K&R.) *In any event, defining a type is
different than defining an object or function. *struct t {...} x; is a
definition. *It defines the object x and reserves space for it. *

All of which proves that the word "define" and words derived from it
mean different things when used to describe the creation of objects or
used to describe other aspects of the language.

This is not that unusual. *The word "token" means different things
when talking about the preprocessor, language syntax, or the use of
the standard function strtok. *Context is important when trying to
decide what things mean.

thank you Barry. That does clarify it.

Aug 19 '08 #5
mdh said:

<snip>
So, I made up this little bit of code.

#include <stdio.h>

int main (int argc, const char * argv[]) {
int foo (int); /* first declaration of foo */

int x = foo (7);
int foo (int); /* 2nd declaration of foo */
/* no error */
It's a function declaration. No problem there.
struct name { /* first declaration of struct */
int x;
};

struct name { /*error : Redefinition of struct name */
int x;
};
I think the reason you're confused may be that you are conflating two
different meanings of the word "definition". The meaning we've been
discussing recently is the definition of an identifier to describe an
object. But:

struct name { int x; };

doesn't define an object - it defines a *type*. Within a given scope, you
can only define a type once.
From Ben's note, I expected to get a re-declaration error, (for the
struct) not a redefinition error.
Firstly, the Standard doesn't place any limits on the wording of diagnostic
messages. (Fortunately, most implementations behave reasonably sanely,
however.)

Secondly, it /is/ a re-definition error, insofar as you've tried to define
the same type twice.
Moreover, if the declaration is
placed at block level, and a second declaration is placed at file
level, there is not error generated.
Different scope.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 19 '08 #6
mdh
On Aug 18, 10:36*pm, Richard Heathfield <r...@see.sig.invalidwrote:
mdh said:

struct name { */* first declaration of struct */
int x;
};
struct name { */*error : Redefinition of struct name */
int x;
};

I think the reason you're confused may be that you are conflating two
different meanings of the word "definition". The meaning we've been
discussing recently is the definition of an identifier to describe an
object. But:

struct name { int x; };
doesn't define an object - it defines a *type*. Within a given scope, you
can only define a type once.
Aha...that's what I have been missing. Thank you Richard.
Aug 19 '08 #7
mdh
On Aug 18, 10:36*pm, Richard Heathfield <r...@see.sig.invalidwrote:
mdh said:

<snip>
So, I made up this little bit of code.
#include <stdio.h>

struct name { */* first declaration of struct */
int x;
};
struct name { */*error : Redefinition of struct name */
int x;
};

I think the reason you're confused may be that you are conflating two
different meanings of the word "definition". The meaning we've been
discussing recently is the definition of an identifier to describe an
object. But:

struct name { int x; };

doesn't define an object - it defines a *type*. Within a given scope, you
can only define a type once.


As I got a good night's sleep, I **think** I see one of the issues
that has always puzzled me. As soon as one uses the word "definition"
C is very strict in enforcing the rule of only 1 definition per scope
and why is this rule in place. If that is indeed true, then perhaps
the logic for this is as follows?

A definition sets aside a named space for that object/type.

So, if more than one definition of the same type/object were to be
allowed, this will lead to ambiguity when using that definition ie
which "space" to use when calling that object/using that type?

Secondly, my thought was , consistent with the error I have often
seen, "Error. Redefinition of ...etc etc", why not then just set the
"old" definition to the "new" one. I guess the answer is really
obvious, but certainly one of them would be...

What would happen to the previously used data when you simply redefine
it...it would be unworkable.
Thanks in advance for something that has always been a puzzle.

Aug 19 '08 #8
mdh <md**@comcast.netwrites:
[...]
As I got a good night's sleep, I **think** I see one of the issues
that has always puzzled me. As soon as one uses the word "definition"
C is very strict in enforcing the rule of only 1 definition per scope
and why is this rule in place. If that is indeed true, then perhaps
the logic for this is as follows?

A definition sets aside a named space for that object/type.
[...]

A type declaration/definition doesn't set aside any space.

--
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"
Aug 19 '08 #9
mdh
On Aug 19, 9:38*am, Keith Thompson <ks...@mib.orgwrote:
mdh <m...@comcast.netwrites:

[...]As I got a good night's sleep, I **think** I see one of the issues
that has always puzzled me. As soon as one uses the word "definition"
C is very strict in enforcing the rule of only 1 definition per scope
and why is this rule in place. If that is indeed true, then perhaps
the logic for this is as follows?
A definition sets aside a named space for that object/type.

[...]

A type declaration/definition doesn't set aside any space.

--
Well, there is something about the issue of "definitions" that C
strictly enforces. That is what I am trying to figure out.:-)
Aug 19 '08 #10
mdh
On Aug 19, 9:38*am, Keith Thompson <ks...@mib.orgwrote:
mdh <m...@comcast.netwrites:

[...]As I got a good night's sleep, I **think** I see one of the issues
that has always puzzled me. As soon as one uses the word "definition"
C is very strict in enforcing the rule of only 1 definition per scope
and why is this rule in place. If that is indeed true, then perhaps
the logic for this is as follows?
A definition sets aside a named space for that object/type.

[...]

A type declaration/definition doesn't set aside any space.

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


Sorry if my flippant reply to Keith put off further input...if so, it
was not meant that way. As i have said, this aspect has always puzzled
me.
Aug 20 '08 #11
mdh <md**@comcast.netwrites:
On Aug 19, 9:38Â*am, Keith Thompson <ks...@mib.orgwrote:
>mdh <m...@comcast.netwrites:

[...]As I got a good night's sleep, I **think** I see one of the issues
that has always puzzled me. As soon as one uses the word "definition"
C is very strict in enforcing the rule of only 1 definition per scope
and why is this rule in place. If that is indeed true, then perhaps
the logic for this is as follows?
A definition sets aside a named space for that object/type.

[...]

A type declaration/definition doesn't set aside any space.

Sorry if my flippant reply to Keith put off further input...if so, it
was not meant that way. As i have said, this aspect has always puzzled
me.
I doubt it was that. I suspect the silence reflects the fact that
questions of the form "Why does C have this rule?" can be very hard to
answer. The rules of C evolved over a period of about decade as a
compromise between convenience and usability for both compiler writers
and compiler users, on a set of machines that most people can't even
remember any more.

My guess as to the reason why you can't redefine a struct tag in the
same scope? There is no significant need, and detecting (and
rejecting) a re-definition is easy for the compiler.

--
Ben.
Aug 20 '08 #12
mdh
mdh <m...@comcast.netwrites:
mdh <m...@comcast.netwrites:
[...]As I got a good night's sleep, I **think** I see one of the issues
that has always puzzled me. As soon as one uses the word "definition"
C is very strict in enforcing the rule of only 1 definition per scope
and why is this rule in place. If that is indeed true, then perhaps
the logic for this is as follows?
A definition sets aside a named space for that object/type.
[...]
On Aug 19, 9:38 am, Keith Thompson <ks...@mib.orgwrote:
A type declaration/definition doesn't set aside any space.
On Aug 20, 9:18 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote
>
My guess as to the reason why you can't redefine a struct tag in the
same scope? *There is no significant need, and detecting (and
rejecting) a re-definition is easy for the compiler.

thanks Ben. I guess, for those who like to think there is a logical
reason for rules, C has the occasional rule that just is the way it
is! I **thought** I had stumbled onto something when it seemed that
anything "defined" , be it type of object, seemed to follow this rule
about not being allowed more than one definition...and extended that
thought with some logic...which in the end is neither here nor there,
as most established programmers just know it.
Thanks again for replying.
Aug 20 '08 #13
mdh <md**@comcast.netwrites:
On Aug 19, 9:38*am, Keith Thompson <ks...@mib.orgwrote:
>mdh <m...@comcast.netwrites:

[...]As I got a good night's sleep, I **think** I see one of the issues
that has always puzzled me. As soon as one uses the word "definition"
C is very strict in enforcing the rule of only 1 definition per scope
and why is this rule in place. If that is indeed true, then perhaps
the logic for this is as follows?
A definition sets aside a named space for that object/type.

[...]

A type declaration/definition doesn't set aside any space.

Sorry if my flippant reply to Keith put off further input...if so, it
was not meant that way. As i have said, this aspect has always puzzled
me.
I didn't answer because I didn't really have anything to add, and I
though others were answering well enough (and I was too lazy to
construct a decent answer anyway). I don't remember what you write,
but it didn't strike me as excessively flippant.

BTW, please don't quote signatures unless you're actually commenting
on them.

--
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"
Aug 20 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wes | last post by:
I am trying to secure different files, mostly pdf, so only the person suppose to see the file that was designed for that individual can see it. I am using sessions to secure the actual web pages,...
3
by: John D. Sanders | last post by:
I have just upgraded MySQL from version 3.23 to version 4.1.8 and now I am getting the following error when I try to run a script that worked: install_driver(mysql) failed: Can't load...
4
by: bbass | last post by:
thanks to all that replyied to my previous post with the following code in question: <a href="merc.htm" target="_new_merc" onfocusout=window.close class="left_link"> i understand that the...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
19
by: Rob | last post by:
I've just started writing managed C++ (VS 2005 Beta 2) and I really have not read a whole lot about the differences between managed and unmanaged C++. Using the class wizard, I can create a...
9
by: Carify Me | last post by:
Hi I am new for developing component. Please advise me for the following problem. I have created the component (DLL) for opening dbconnection Using Oracle, using the below statement to use...
3
by: rodchar | last post by:
hey all, i'm reading about app architecture in patterns and practices and there was a paragraph i need some clarification on. please keep in mind that i'm new to this object oriented stuff so...
9
by: op | last post by:
Is there any difference between these two ways of declaring a pointer? 1) int* a; // a is a pointer where an integer is stored 2) int *a; // value stored in a is an integer. Can we use...
2
by: sarathy | last post by:
Hi all, I have a clarification in C++ Templates. Consider that we have created a template as below template <class T1,class T2, class T3> void real_test(T1 a, T2 b ,T3 c) { const char *x=a;...
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.