declaration of just a type with storage class specifier

  • Follow


In the book "C A reference Manual" (5th edition), the authors state
that a declaration like the following is invalid:

static struct s { int a; int b; }   ;

I can't find in the standard which constraint or semantics is
violated.
Can you help?
0
Reply Luca 8/1/2010 3:48:31 PM

Luca Forlizzi wrote:
> In the book "C A reference Manual" (5th edition), the authors state
> that a declaration like the following is invalid:
>
> static struct s { int a; int b; }   ;
>
> I can't find in the standard which constraint or semantics is
> violated.
> Can you help?

'static' should be applied to declarations with declarators.
You are just declaring a 'tag', not a variable of type struct,
so it has not sense to apply a storage class specifier

-- 
Vincenzo Mercuri
0
Reply Vincenzo 8/1/2010 3:55:49 PM


Vincenzo Mercuri wrote:


> so it has not sense [...]

sorry, i meant 'it makes no sense'



-- 
Vincenzo Mercuri
0
Reply Vincenzo 8/1/2010 3:59:23 PM

On 1 Ago, 17:59, Vincenzo Mercuri <c...@lang.c> wrote:
> Vincenzo Mercuri wrote:
> > so it has not sense [...]
>
> sorry, i meant 'it makes no sense'
>
> --
> Vincenzo Mercuri

I agree that it makes no sense, but how one concludes that it is
invalid?
0
Reply Luca 8/1/2010 4:08:59 PM

Luca Forlizzi <luca.forlizzi@gmail.com> writes:

> In the book "C A reference Manual" (5th edition), the authors state
> that a declaration like the following is invalid:
>
> static struct s { int a; int b; }   ;
>
> I can't find in the standard which constraint or semantics is
> violated.
> Can you help?

My best understanding is that it is legal and well-defined.
Certainly it's legal and well-defined without the 'static'
storage-class specifier, and a grep on 'storage.class' didn't
turn up anything wrong that I could see.
0
Reply Tim 8/1/2010 4:11:26 PM

Luca Forlizzi wrote:
> On 1 Ago, 17:59, Vincenzo Mercuri<c...@lang.c>  wrote:
>> Vincenzo Mercuri wrote:
>>> so it has not sense [...]
>>
>> sorry, i meant 'it makes no sense'
>>
>> --
>> Vincenzo Mercuri
>
> I agree that it makes no sense, but how one concludes that it is
> invalid?

Beware that 'C: A Reference Manual' says that it is
"nonsensical and (in Standard C) illegal" where Standard C
means C89 (see page 4, 5th edition).
Now I can't say if it is illegal in C89, (i dont have the
Standard) but I can just say it is nonsensical in C99

-- 
Vincenzo Mercuri
0
Reply Vincenzo 8/1/2010 4:23:31 PM

On 1 Ago, 18:11, Tim Rentsch <t...@alumni.caltech.edu> wrote:
> Luca Forlizzi <luca.forli...@gmail.com> writes:
> > In the book "C A reference Manual" (5th edition), the authors state
> > that a declaration like the following is invalid:
>
> > static struct s { int a; int b; } =A0 ;
>
> > I can't find in the standard which constraint or semantics is
> > violated.
> > Can you help?
>
> My best understanding is that it is legal and well-defined.
> Certainly it's legal and well-defined without the 'static'
> storage-class specifier, and a grep on 'storage.class' didn't
> turn up anything wrong that I could see.

that's also my understanding. I also think that the presence of
'static' has no effect on the semantics of the declaration (as
suggested by the book authors). Do you agree?
0
Reply Luca 8/1/2010 4:54:50 PM

On 1 Ago, 18:23, Vincenzo Mercuri <c...@lang.c> wrote:
> Luca Forlizzi wrote:
> > On 1 Ago, 17:59, Vincenzo Mercuri<c...@lang.c> =A0wrote:
> >> Vincenzo Mercuri wrote:
> >>> so it has not sense [...]
>
> >> sorry, i meant 'it makes no sense'
>
> >> --
> >> Vincenzo Mercuri
>
> > I agree that it makes no sense, but how one concludes that it is
> > invalid?
>
> Beware that 'C: A Reference Manual' says that it is
> "nonsensical and (in Standard C) illegal" where Standard C
> means C89 (see page 4, 5th edition).
> Now I can't say if it is illegal in C89, (i dont have the
> Standard) but I can just say it is nonsensical in C99
>
> --
> Vincenzo Mercuri

Hi Vincenzo,

 I checked C89 and I don't see in it anything that renders such a
declaration invalid. I think it is valid and a well-defined
declaration.
And that it has a meaning, because it declares the new struct type and
its tag.
Because of that, I wouldn't call it nonsensical. The only
"nonsensical" part, is that it uses a storage-class specifier that
could be omitted without changing the declaration's meaning. There are
cases like this. For instance in

extern int f(void);

the storage-class specifier could be omitted without changing
semantics, as well (so I think).
Would you call it nonsensical, too ?
0
Reply Luca 8/2/2010 1:47:30 PM

Luca Forlizzi wrote:
> On 1 Ago, 18:23, Vincenzo Mercuri<c...@lang.c>  wrote:
>> Luca Forlizzi wrote:
>>> On 1 Ago, 17:59, Vincenzo Mercuri<c...@lang.c>    wrote:
>>>> Vincenzo Mercuri wrote:
>>>>> so it has not sense [...]
>>
>>>> sorry, i meant 'it makes no sense'
>>
>>>> --
>>>> Vincenzo Mercuri
>>
>>> I agree that it makes no sense, but how one concludes that it is
>>> invalid?
>>
>> Beware that 'C: A Reference Manual' says that it is
>> "nonsensical and (in Standard C) illegal" where Standard C
>> means C89 (see page 4, 5th edition).
>> Now I can't say if it is illegal in C89, (i dont have the
>> Standard) but I can just say it is nonsensical in C99
>>
>> --
>> Vincenzo Mercuri
>
> Hi Vincenzo,
>
>   I checked C89 and I don't see in it anything that renders such a
> declaration invalid. I think it is valid and a well-defined
> declaration.
> And that it has a meaning, because it declares the new struct type and
> its tag.
> Because of that, I wouldn't call it nonsensical. The only
> "nonsensical" part, is that it uses a storage-class specifier that
> could be omitted without changing the declaration's meaning. There are
> cases like this. For instance in
>
> extern int f(void);
>
> the storage-class specifier could be omitted without changing
> semantics, as well (so I think).
> Would you call it nonsensical, too ?

Hi Luca,

	...I think I got your point. My answer is: no, I wouldn't call it
'nonsensical' and I'm going to tell you why. The 'extern' storage-class
specifier is unnecessary in function prototype declarations, but
unnecessary doesn't mean nonsensical. For some programmers it's
just good style. Something different happens with:

	static struct s { int a; int b; };
	
here the problem is that the storage-class specifier will be ignored
by the compiler (see H&S pag 89) -- gcc warns you: 'useless storage
class specifier in empty declaration'.
Again, as H&S says:
"you might think that a later declaration of the form":

	struct s x, y;
	
"will cause x and y to have the storage class static. It will not."


-- 
Vincenzo Mercuri
0
Reply Vincenzo 8/2/2010 4:30:46 PM

Luca Forlizzi <luca.forlizzi@gmail.com> writes:

> On 1 Ago, 18:11, Tim Rentsch <t...@alumni.caltech.edu> wrote:
>> Luca Forlizzi <luca.forli...@gmail.com> writes:
>> > In the book "C A reference Manual" (5th edition), the authors state
>> > that a declaration like the following is invalid:
>>
>> > static struct s { int a; int b; }   ;
>>
>> > I can't find in the standard which constraint or semantics is
>> > violated.
>> > Can you help?
>>
>> My best understanding is that it is legal and well-defined.
>> Certainly it's legal and well-defined without the 'static'
>> storage-class specifier, and a grep on 'storage.class' didn't
>> turn up anything wrong that I could see.
>
> that's also my understanding. I also think that the presence of
> 'static' has no effect on the semantics of the declaration (as
> suggested by the book authors). Do you agree?

Yes, reading the relevant sections (6.2.2 and 6.2.4, IIRC), makes
it clear that the result is not changed by adding 'static'.
0
Reply Tim 8/4/2010 6:27:03 PM

On Aug 4, 2:27=A0pm, Tim Rentsch <t...@alumni.caltech.edu> wrote:
> Luca Forlizzi <luca.forli...@gmail.com> writes:
> > On 1 Ago, 18:11, Tim Rentsch <t...@alumni.caltech.edu> wrote:
> >> Luca Forlizzi <luca.forli...@gmail.com> writes:
> >> > In the book "C A reference Manual" (5th edition), the authors state
> >> > that a declaration like the following is invalid:
>
> >> > static struct s { int a; int b; } =A0 ;
>
> >> > I can't find in the standard which constraint or semantics is
> >> > violated.
> >> > Can you help?
>
> >> My best understanding is that it is legal and well-defined.
> >> Certainly it's legal and well-defined without the 'static'
> >> storage-class specifier, and a grep on 'storage.class' didn't
> >> turn up anything wrong that I could see.
>
> > that's also my understanding. I also think that the presence of
> > 'static' has no effect on the semantics of the declaration (as
> > suggested by the book authors). Do you agree?
>
> Yes, reading the relevant sections (6.2.2 and 6.2.4, IIRC), makes
> it clear that the result is not changed by adding 'static'.
Agreed.  Functions and objects have linkage.  Objects have storage
duration.  A 'struct' tag has neither of these, but "storage class
specifier" in the syntax is (ab)used to include 'typedef'.  One might
wish to use 'typedef' along with their 'struct' declaration.
0
Reply Shao 8/5/2010 5:53:09 PM

10 Replies
505 Views

(page loaded in 0.132 seconds)

Similiar Articles:













7/24/2012 5:15:42 AM


Reply: