Is this a valid struct declaration

R

Raman

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
 
R

rahul

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

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.
 
B

Ben Bacarisse

rahul said:
On Jun 6, 2:32 pm, Raman <[email protected]> wrote:

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.
 
F

Flash Gordon

rahul said:
No.

No.


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;
 
M

muks

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.

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
 
D

Dan

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.
 
I

Ian Collins

rahul said:
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.

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

Raman

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!


Wrong. From N1256:


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)


See above.

An imcomplete type would be
struct test;

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
 
F

Flash Gordon

Raman wrote, On 09/06/08 10:41:

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top