Declaration does not declare anything error

P

prix prad

Hi,

Error comes when you have:
--------------------------
struct foo_1 {
struct abc {
int a;
int b;
};
int c;
};

The below solves this error:
-----------------------------
struct foo_1 {
struct {
int a;
int b;
} abc;
int c;
};

Why does not is occur in the first place and why does the reshuffling
resolve it?

Pritam
 
K

Keith Thompson

prix prad said:
Error comes when you have:
--------------------------
struct foo_1 {
struct abc {
int a;
int b;
};
int c;
};

"struct abc { int a; int b; }" is a type; it can be referred to as
"struct abc". You don't declare anything of that type. The only
things that should appear between the "{" and "}" of a struct
declaration (in this case, the declaration of struct foo_1) are member
declarations. If "struct abc { int a; int b; };" appeared outside the
struct declaration, it would be a legitimate declaration of a struct
type, but it can't legally appear where you've placed it.

What exactly do you expect the above to mean?
The below solves this error:
-----------------------------
struct foo_1 {
struct {
int a;
int b;
} abc;
int c;
};

Here the name "abc" means something quite different from what it meant
in the first example. "struct { int a; int b; }" is still a type; the
difference is that it has no tag. Such a declaration by itself would
not be useful, since you wouldn't be either declaring an object of the
type or providing a name by which you can refer to the type. But by
adding "abc;", you're declaring an object (actually, in this context,
a struct member) of that type.

Here's a better (IMHO) way to do what you're doong here:

struct inner {
int a;
int b;
};

struct foo_1 {
struct inner abc;
int c;
};

Of course in real life you'd use descriptive names for everything.
 
J

James Kuyper

prix said:
Hi,

Error comes when you have:
--------------------------
struct foo_1 {
struct abc {
int a;
int b;
};

This defines a struct type with tag name of abc. It does not declare any
objects of that type.
int c;
};

Putting the definition of struct abc inside the definition of struct
foo_1 was pointless, because the above declaration is functionally
identical to the following, simpler declaration:

struct abc {
int a;
int b;
};
struct foo_1 {
int c;
};

There's also relatively little point in declaring a structure with only
one element, unless that element has array type.
The below solves this error:
-----------------------------
struct foo_1 {
struct {
int a;
int b;
} abc;

This defines a struct type with no tag name, and an object of that type
named abc.
int c;
};

Why does not is occur in the first place and why does the reshuffling
resolve it?

The first declaration was pointless, but in itself it doesn't violate
any rules. Therefore, the error messages presumably come from somewhere
else. It would help if you could show the actual code where the error
was detected, and the actual text of the error message.

Without that information, all I can say is that abc has two wildly
different meanings in the two declarations. As a result, code which
makes correct use of the first declaration would generally be
incompatible with code that makes correct use of the second one, and
vice versa.
 
P

Pritam

The only
things that should appear between the "{" and "}" of a struct
declaration (in this case, the declaration of struct foo_1) are member
declarations.
Here the name "abc" means something quite different from what it meant
in the first example. "struct { int a; int b; }" is still a type; the
difference is that it has no tag. Such a declaration by itself would
not be useful, since you wouldn't be either declaring an object of the
type or providing a name by which you can refer to the type. But by
adding "abc;", you're declaring an object (actually, in this context,
a struct member) of that type.

Ah, that makes perfect sense. Thanks a bunch!
Prix
 
K

Keith Thompson

James Kuyper said:
prix said:
Error comes when you have:
--------------------------
struct foo_1 {
struct abc {
int a;
int b;
};

This defines a struct type with tag name of abc. It does not declare
any objects of that type.
int c;
};

Putting the definition of struct abc inside the definition of struct
foo_1 was pointless, because the above declaration is functionally
identical to the following, simpler declaration:

struct abc {
int a;
int b;
};
struct foo_1 {
int c;
}; [...]
The first declaration was pointless, but in itself it doesn't violate
any rules. Therefore, the error messages presumably come from
somewhere else. It would help if you could show the actual code where
the error was detected, and the actual text of the error message.
[...]

Actually I think it is illegal. If I'm reading C99 6.7.2.1 correctly,
a struct type declaration that doesn't declare a member violates the
grammar.
 
J

James Kuyper

Keith said:
James Kuyper said:
prix said:
Error comes when you have:
--------------------------
struct foo_1 {
struct abc {
int a;
int b;
};
This defines a struct type with tag name of abc. It does not declare
any objects of that type.
int c;
};
Putting the definition of struct abc inside the definition of struct
foo_1 was pointless, because the above declaration is functionally
identical to the following, simpler declaration:

struct abc {
int a;
int b;
};
struct foo_1 {
int c;
}; [...]
The first declaration was pointless, but in itself it doesn't violate
any rules. Therefore, the error messages presumably come from
somewhere else. It would help if you could show the actual code where
the error was detected, and the actual text of the error message.
[...]

Actually I think it is illegal. If I'm reading C99 6.7.2.1 correctly,
a struct type declaration that doesn't declare a member violates the
grammar.

Your response to prix led me to check that same section and reconsider
my comments, and I think you're right. C lets you do a lot of pointless
things; it's nice to know that there are few that it doesn't let you do.
 
A

Andrey Tarasevich

prix said:
Error comes when you have:
--------------------------
struct foo_1 {
struct abc {
int a;
int b;
};
int c;
};

The below solves this error:
-----------------------------
struct foo_1 {
struct {
int a;
int b;
} abc;
int c;
};

Why does not is occur in the first place and why does the reshuffling
resolve it?

It is not "reshuffling". It is a major modification that completely
changes the meaning of the declaration. The above two pieces of code
have very little in common.

In C language, whatever you put between the '{ ... }' in a declaration
of a 'struct' type is required to declare a _member_ (a data field) of
that struct type.

In your first case the nested declaration of 'struct abc' does not
declare any member of 'foo_1', So, your code is broken.

In your second case you are declaring a member 'abc' of a nameless
struct type. This is fine.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top