What does it mean when define a struct var without identifier in a structure in c language?
up vote
1
down vote
favorite
In C language, what is the meaning of struct Animal;
in line # 6?
Is it legal in C89 or C99 or C11?
struct Animal {
char *name;
int age;
};
struct Cat {
struct Animal; // line 6
int category;
};
Thanks!
c structure
add a comment |
up vote
1
down vote
favorite
In C language, what is the meaning of struct Animal;
in line # 6?
Is it legal in C89 or C99 or C11?
struct Animal {
char *name;
int age;
};
struct Cat {
struct Animal; // line 6
int category;
};
Thanks!
c structure
I think it says "there is a typestruct Animal
", and that's all. It doesn't do anything useful because line 1 already said that. When I compile it, I getx.c:9:18: error: declaration does not declare anything [-Werror]
—struct Animal;
which is what I'd expect. It would be a warning if I didn't compile with-Werror
.
– Jonathan Leffler
Nov 20 at 4:54
I've tried the code in VS2015 with a .c file, and it compiled ok, and output: sizeof(struct Animal): 8 sizeof(struct Cat): 12 which looks like there is an "struct Animal" entity in struct cat. So I'm not sure is it legal in C89 or C99?
– Ring.Yee
Nov 20 at 5:01
It's dubious, but legal. I have GCC set to very fussy, and any warnings it produces are treated as errors.
– Jonathan Leffler
Nov 20 at 5:02
In clang, it looks like meaning nothing, I've tried the code in xcode9, and it outputs: sizeof(struct Animal): 16 sizeof(struct Cat): 4
– Ring.Yee
Nov 20 at 5:08
Working on a Mac, 64-bit compilation, and suppressing the-Werror
option, I get size of Animal as 16 and size of Cat as 4, which is exactly what I'd expect. Your result on VS2015 is curious — I find it hard to explain how you got that result.
– Jonathan Leffler
Nov 20 at 5:14
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In C language, what is the meaning of struct Animal;
in line # 6?
Is it legal in C89 or C99 or C11?
struct Animal {
char *name;
int age;
};
struct Cat {
struct Animal; // line 6
int category;
};
Thanks!
c structure
In C language, what is the meaning of struct Animal;
in line # 6?
Is it legal in C89 or C99 or C11?
struct Animal {
char *name;
int age;
};
struct Cat {
struct Animal; // line 6
int category;
};
Thanks!
c structure
c structure
edited Nov 20 at 5:39
Azeem
2,85041023
2,85041023
asked Nov 20 at 4:49
Ring.Yee
214
214
I think it says "there is a typestruct Animal
", and that's all. It doesn't do anything useful because line 1 already said that. When I compile it, I getx.c:9:18: error: declaration does not declare anything [-Werror]
—struct Animal;
which is what I'd expect. It would be a warning if I didn't compile with-Werror
.
– Jonathan Leffler
Nov 20 at 4:54
I've tried the code in VS2015 with a .c file, and it compiled ok, and output: sizeof(struct Animal): 8 sizeof(struct Cat): 12 which looks like there is an "struct Animal" entity in struct cat. So I'm not sure is it legal in C89 or C99?
– Ring.Yee
Nov 20 at 5:01
It's dubious, but legal. I have GCC set to very fussy, and any warnings it produces are treated as errors.
– Jonathan Leffler
Nov 20 at 5:02
In clang, it looks like meaning nothing, I've tried the code in xcode9, and it outputs: sizeof(struct Animal): 16 sizeof(struct Cat): 4
– Ring.Yee
Nov 20 at 5:08
Working on a Mac, 64-bit compilation, and suppressing the-Werror
option, I get size of Animal as 16 and size of Cat as 4, which is exactly what I'd expect. Your result on VS2015 is curious — I find it hard to explain how you got that result.
– Jonathan Leffler
Nov 20 at 5:14
add a comment |
I think it says "there is a typestruct Animal
", and that's all. It doesn't do anything useful because line 1 already said that. When I compile it, I getx.c:9:18: error: declaration does not declare anything [-Werror]
—struct Animal;
which is what I'd expect. It would be a warning if I didn't compile with-Werror
.
– Jonathan Leffler
Nov 20 at 4:54
I've tried the code in VS2015 with a .c file, and it compiled ok, and output: sizeof(struct Animal): 8 sizeof(struct Cat): 12 which looks like there is an "struct Animal" entity in struct cat. So I'm not sure is it legal in C89 or C99?
– Ring.Yee
Nov 20 at 5:01
It's dubious, but legal. I have GCC set to very fussy, and any warnings it produces are treated as errors.
– Jonathan Leffler
Nov 20 at 5:02
In clang, it looks like meaning nothing, I've tried the code in xcode9, and it outputs: sizeof(struct Animal): 16 sizeof(struct Cat): 4
– Ring.Yee
Nov 20 at 5:08
Working on a Mac, 64-bit compilation, and suppressing the-Werror
option, I get size of Animal as 16 and size of Cat as 4, which is exactly what I'd expect. Your result on VS2015 is curious — I find it hard to explain how you got that result.
– Jonathan Leffler
Nov 20 at 5:14
I think it says "there is a type
struct Animal
", and that's all. It doesn't do anything useful because line 1 already said that. When I compile it, I get x.c:9:18: error: declaration does not declare anything [-Werror]
— struct Animal;
which is what I'd expect. It would be a warning if I didn't compile with -Werror
.– Jonathan Leffler
Nov 20 at 4:54
I think it says "there is a type
struct Animal
", and that's all. It doesn't do anything useful because line 1 already said that. When I compile it, I get x.c:9:18: error: declaration does not declare anything [-Werror]
— struct Animal;
which is what I'd expect. It would be a warning if I didn't compile with -Werror
.– Jonathan Leffler
Nov 20 at 4:54
I've tried the code in VS2015 with a .c file, and it compiled ok, and output: sizeof(struct Animal): 8 sizeof(struct Cat): 12 which looks like there is an "struct Animal" entity in struct cat. So I'm not sure is it legal in C89 or C99?
– Ring.Yee
Nov 20 at 5:01
I've tried the code in VS2015 with a .c file, and it compiled ok, and output: sizeof(struct Animal): 8 sizeof(struct Cat): 12 which looks like there is an "struct Animal" entity in struct cat. So I'm not sure is it legal in C89 or C99?
– Ring.Yee
Nov 20 at 5:01
It's dubious, but legal. I have GCC set to very fussy, and any warnings it produces are treated as errors.
– Jonathan Leffler
Nov 20 at 5:02
It's dubious, but legal. I have GCC set to very fussy, and any warnings it produces are treated as errors.
– Jonathan Leffler
Nov 20 at 5:02
In clang, it looks like meaning nothing, I've tried the code in xcode9, and it outputs: sizeof(struct Animal): 16 sizeof(struct Cat): 4
– Ring.Yee
Nov 20 at 5:08
In clang, it looks like meaning nothing, I've tried the code in xcode9, and it outputs: sizeof(struct Animal): 16 sizeof(struct Cat): 4
– Ring.Yee
Nov 20 at 5:08
Working on a Mac, 64-bit compilation, and suppressing the
-Werror
option, I get size of Animal as 16 and size of Cat as 4, which is exactly what I'd expect. Your result on VS2015 is curious — I find it hard to explain how you got that result.– Jonathan Leffler
Nov 20 at 5:14
Working on a Mac, 64-bit compilation, and suppressing the
-Werror
option, I get size of Animal as 16 and size of Cat as 4, which is exactly what I'd expect. Your result on VS2015 is curious — I find it hard to explain how you got that result.– Jonathan Leffler
Nov 20 at 5:14
add a comment |
1 Answer
1
active
oldest
votes
up vote
5
down vote
It doesn't mean much of anything -- and in fact it's a constraint violation.
The language grammar allows
struct Animal;
in place of a member declaration inside a struct or union declaration. But it violates a language rule (N1570 6.7.2.1 paragraph 2):
A struct-declaration that does not declare an anonymous structure or
anonymous union shall contain a struct-declarator-list.
This is a "constraint", which means that violating it requires a diagnostic. (A non-fatal warning message qualifies as a diagnostic.)
If you had written:
struct Cat {
struct Animal foo;
int category;
};
then the foo
would be a "declarator". The constraint I quoted above means you're not allowed to omit it. gcc and clang both warn about this by default ("declaration does not declare anything") and reject it with -Wpedantic-errors
.
(Anonymous structs and unions were added to the language in C11, and are discussed here, but the code in your question is not an anonymous struct.)
1
-pedantic-errors
makes GCC reject the code, so I'm not sure if it's actually allowed.
– HolyBlackCat
Nov 20 at 5:43
@HolyBlackCat: Good catch. I've updated my answer.
– Keith Thompson
Nov 20 at 7:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386410%2fwhat-does-it-mean-when-define-a-struct-var-without-identifier-in-a-structure-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
It doesn't mean much of anything -- and in fact it's a constraint violation.
The language grammar allows
struct Animal;
in place of a member declaration inside a struct or union declaration. But it violates a language rule (N1570 6.7.2.1 paragraph 2):
A struct-declaration that does not declare an anonymous structure or
anonymous union shall contain a struct-declarator-list.
This is a "constraint", which means that violating it requires a diagnostic. (A non-fatal warning message qualifies as a diagnostic.)
If you had written:
struct Cat {
struct Animal foo;
int category;
};
then the foo
would be a "declarator". The constraint I quoted above means you're not allowed to omit it. gcc and clang both warn about this by default ("declaration does not declare anything") and reject it with -Wpedantic-errors
.
(Anonymous structs and unions were added to the language in C11, and are discussed here, but the code in your question is not an anonymous struct.)
1
-pedantic-errors
makes GCC reject the code, so I'm not sure if it's actually allowed.
– HolyBlackCat
Nov 20 at 5:43
@HolyBlackCat: Good catch. I've updated my answer.
– Keith Thompson
Nov 20 at 7:03
add a comment |
up vote
5
down vote
It doesn't mean much of anything -- and in fact it's a constraint violation.
The language grammar allows
struct Animal;
in place of a member declaration inside a struct or union declaration. But it violates a language rule (N1570 6.7.2.1 paragraph 2):
A struct-declaration that does not declare an anonymous structure or
anonymous union shall contain a struct-declarator-list.
This is a "constraint", which means that violating it requires a diagnostic. (A non-fatal warning message qualifies as a diagnostic.)
If you had written:
struct Cat {
struct Animal foo;
int category;
};
then the foo
would be a "declarator". The constraint I quoted above means you're not allowed to omit it. gcc and clang both warn about this by default ("declaration does not declare anything") and reject it with -Wpedantic-errors
.
(Anonymous structs and unions were added to the language in C11, and are discussed here, but the code in your question is not an anonymous struct.)
1
-pedantic-errors
makes GCC reject the code, so I'm not sure if it's actually allowed.
– HolyBlackCat
Nov 20 at 5:43
@HolyBlackCat: Good catch. I've updated my answer.
– Keith Thompson
Nov 20 at 7:03
add a comment |
up vote
5
down vote
up vote
5
down vote
It doesn't mean much of anything -- and in fact it's a constraint violation.
The language grammar allows
struct Animal;
in place of a member declaration inside a struct or union declaration. But it violates a language rule (N1570 6.7.2.1 paragraph 2):
A struct-declaration that does not declare an anonymous structure or
anonymous union shall contain a struct-declarator-list.
This is a "constraint", which means that violating it requires a diagnostic. (A non-fatal warning message qualifies as a diagnostic.)
If you had written:
struct Cat {
struct Animal foo;
int category;
};
then the foo
would be a "declarator". The constraint I quoted above means you're not allowed to omit it. gcc and clang both warn about this by default ("declaration does not declare anything") and reject it with -Wpedantic-errors
.
(Anonymous structs and unions were added to the language in C11, and are discussed here, but the code in your question is not an anonymous struct.)
It doesn't mean much of anything -- and in fact it's a constraint violation.
The language grammar allows
struct Animal;
in place of a member declaration inside a struct or union declaration. But it violates a language rule (N1570 6.7.2.1 paragraph 2):
A struct-declaration that does not declare an anonymous structure or
anonymous union shall contain a struct-declarator-list.
This is a "constraint", which means that violating it requires a diagnostic. (A non-fatal warning message qualifies as a diagnostic.)
If you had written:
struct Cat {
struct Animal foo;
int category;
};
then the foo
would be a "declarator". The constraint I quoted above means you're not allowed to omit it. gcc and clang both warn about this by default ("declaration does not declare anything") and reject it with -Wpedantic-errors
.
(Anonymous structs and unions were added to the language in C11, and are discussed here, but the code in your question is not an anonymous struct.)
edited Nov 20 at 7:03
answered Nov 20 at 5:02
Keith Thompson
189k25278466
189k25278466
1
-pedantic-errors
makes GCC reject the code, so I'm not sure if it's actually allowed.
– HolyBlackCat
Nov 20 at 5:43
@HolyBlackCat: Good catch. I've updated my answer.
– Keith Thompson
Nov 20 at 7:03
add a comment |
1
-pedantic-errors
makes GCC reject the code, so I'm not sure if it's actually allowed.
– HolyBlackCat
Nov 20 at 5:43
@HolyBlackCat: Good catch. I've updated my answer.
– Keith Thompson
Nov 20 at 7:03
1
1
-pedantic-errors
makes GCC reject the code, so I'm not sure if it's actually allowed.– HolyBlackCat
Nov 20 at 5:43
-pedantic-errors
makes GCC reject the code, so I'm not sure if it's actually allowed.– HolyBlackCat
Nov 20 at 5:43
@HolyBlackCat: Good catch. I've updated my answer.
– Keith Thompson
Nov 20 at 7:03
@HolyBlackCat: Good catch. I've updated my answer.
– Keith Thompson
Nov 20 at 7:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386410%2fwhat-does-it-mean-when-define-a-struct-var-without-identifier-in-a-structure-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think it says "there is a type
struct Animal
", and that's all. It doesn't do anything useful because line 1 already said that. When I compile it, I getx.c:9:18: error: declaration does not declare anything [-Werror]
—struct Animal;
which is what I'd expect. It would be a warning if I didn't compile with-Werror
.– Jonathan Leffler
Nov 20 at 4:54
I've tried the code in VS2015 with a .c file, and it compiled ok, and output: sizeof(struct Animal): 8 sizeof(struct Cat): 12 which looks like there is an "struct Animal" entity in struct cat. So I'm not sure is it legal in C89 or C99?
– Ring.Yee
Nov 20 at 5:01
It's dubious, but legal. I have GCC set to very fussy, and any warnings it produces are treated as errors.
– Jonathan Leffler
Nov 20 at 5:02
In clang, it looks like meaning nothing, I've tried the code in xcode9, and it outputs: sizeof(struct Animal): 16 sizeof(struct Cat): 4
– Ring.Yee
Nov 20 at 5:08
Working on a Mac, 64-bit compilation, and suppressing the
-Werror
option, I get size of Animal as 16 and size of Cat as 4, which is exactly what I'd expect. Your result on VS2015 is curious — I find it hard to explain how you got that result.– Jonathan Leffler
Nov 20 at 5:14