Re: A clarification please



On Mon, 18 Aug 2008 15:23:06 -0700 (PDT), mdh <mdeh@xxxxxxxxxxx>
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
.



Relevant Pages

  • Re: syntax errror
    ... >>> int maxGrey' ... >> declaration of maxGrey. ... typedef double; ... struct some_tag_name { ...
    (comp.lang.c)
  • Re: void * vs char *
    ... cast p to a (struct s*) to avoid a compile-time error. ... struct s {int a;}; ... function call consists solely of an identifier, and if no declaration ... And see what your compiler has to say. ...
    (comp.lang.c)
  • Re: formal parameter 1 different from declaration?
    ... Here is the typedef one: ... typedef LR (HWND, long, int, int); ... struct tagWnd; // forward declaration ... takes a struct tagHwnd and the other takes a long. ...
    (microsoft.public.vc.language)
  • Re: Passing Structure to a function
    ... > int Menu; ... 'struct' keyword above is because otherwise you got compiler errors. ... The reason for the errors is that phonerec hasn't been declared or ... A structure declaration merely says that a structure ...
    (comp.lang.cpp)
  • Re: Linux 2.6.27.8
    ... struct pt_regs *regs); ... static int printbinary ... void show_regs ...
    (Linux-Kernel)