Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is this a valid struct declaration

1 view
Skip to first unread message

Raman

unread,
Jun 6, 2008, 5:32:03 AM6/6/08
to
Hi All,


Is it valid:

struct test{

};

I mean, Can we have a struct containing no members? Is this a an
incomplete-type?

Please provide your expert comments.

Thanks,
Raman Chalotra

rahul

unread,
Jun 6, 2008, 5:51:44 AM6/6/08
to

I don't see anything wrong with the declaration. Though this stuff
does not look like it can have any practical applications.
The standard does not mandate declaring a member in a structure. This
code compiles with '-ansi' flag on Linux/gcc.
I don't see any reason why it should not be valid.

Ben Bacarisse

unread,
Jun 6, 2008, 7:54:10 AM6/6/08
to
rahul <rahul...@gmail.com> writes:

> On Jun 6, 2:32 pm, Raman <ramanchalo...@gmail.com> wrote:

<snip>


>> struct test{
>>
>> };
>>
>> I mean, Can we have a struct containing no members? Is this a an
>> incomplete-type?
>>
>> Please provide your expert comments.
>>
>> Thanks,
>> Raman Chalotra
>
> I don't see anything wrong with the declaration. Though this stuff
> does not look like it can have any practical applications.
> The standard does not mandate declaring a member in a structure.

My copy does. The syntax specifies that there must be at least one
member. Where are you getting your information from?

> This
> code compiles with '-ansi' flag on Linux/gcc.

Add -pedantic.

> I don't see any reason why it should not be valid.

--
Ben.

Flash Gordon

unread,
Jun 6, 2008, 7:56:01 AM6/6/08
to
rahul wrote:
> On Jun 6, 2:32 pm, Raman <ramanchalo...@gmail.com> wrote:
>> Hi All,
>>
>> Is it valid:
>>
>> struct test{
>>
>> };
>>
>> I mean, Can we have a struct containing no members?

No.

>> Is this a an
>> incomplete-type?

No.

>> Please provide your expert comments.
>>
>> Thanks,
>> Raman Chalotra
>
> I don't see anything wrong with the declaration.

Then you need to look at the standard more carefully:
6.7.2.1 Structure and union specifiers
Syntax
1 struct-or-union-specifier:
struct-or-union identifieropt { struct-declaration-list }
struct-or-union identifier

struct-or-union:
struct
union

struct-declaration-list:
struct-declaration
struct-declaration-list struct-declaration

Note that a struct-declaration-list ALWAYS has a struct-declaration!

> Though this stuff
> does not look like it can have any practical applications.
> The standard does not mandate declaring a member in a structure.

Wrong. From N1256:


> This
> code compiles with '-ansi' flag on Linux/gcc.

That does not make it procude all required diagnostics. You need '-ansi
-pedantic' at which point it gives a warning. Note that a warning is
sufficient to meet the standards requirement for "invalid" code (a puch
in the face could also qualify, but I don't think an implementation that
did this would be very popular)

> I don't see any reason why it should not be valid.

See above.

An imcomplete type would be
struct test;
--
Flash Gordon

muks

unread,
Jun 6, 2008, 9:45:27 AM6/6/08
to

hey i dont find ne thng wrong in ur problem..........bt surely this
thng cannot b realised in practical situation......rest its ok.tk cr
bye

Jack Klein

unread,
Jun 6, 2008, 11:12:15 PM6/6/08
to
On Fri, 6 Jun 2008 06:45:27 -0700 (PDT), muks <mukul...@gmail.com>
wrote in comp.lang.c:

hey ur a fkng mron.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Chris Thomasson

unread,
Jun 7, 2008, 1:14:41 AM6/7/08
to

"muks" <mukul...@gmail.com> wrote in message
news:d913f2f4-92f1-4ec8...@a32g2000prf.googlegroups.com...
[...]

> hey i dont find ne thng wrong in ur problem..........bt surely this
> thng cannot b realised in practical situation......rest its ok.tk cr
> bye

:^/

Dan

unread,
Jun 7, 2008, 8:46:13 PM6/7/08
to

>hey i dont find ne thng wrong in ur problem..........bt surely this
>thng cannot b realised in practical situation......rest its ok.tk cr
>bye

I have seen #defines that turn on and off structure members to save memory,
and one even turned all the members off.


Ian Collins

unread,
Jun 7, 2008, 8:54:35 PM6/7/08
to

Are you sure you weren't compiling as C++?

--
Ian Collins.

Raman

unread,
Jun 9, 2008, 5:41:22 AM6/9/08
to

HI,

Yes, and
1. we cant apply sizeof to incomplete types.
2. what if we create array of this struct(incomplete), every element
having same mem address....which is probably violation of array
concept in itself.

Thanks,
Raman Chalotra

Flash Gordon

unread,
Jun 9, 2008, 5:11:46 PM6/9/08
to
Raman wrote, On 09/06/08 10:41:

<snip>

> Yes, and
> 1. we cant apply sizeof to incomplete types.

True.

> 2. what if we create array of this struct(incomplete), every element
> having same mem address....which is probably violation of array
> concept in itself.

It is an error that the compiler is required to diagnose (produce a
warning, error or tattoo the appropriate clause of the standard on some
part of your body for).
--
Flash Gordon
Inventor of strange and unusual diagnostics

0 new messages