Some questions about struct namespace in C
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to understand this answer - typedef struct vs struct definitions [duplicate]:
(Line 3): ["] In the first line you are defining the identifier S within the struct name space (not in the C++ sense). [."]
- It seems like for
struct X {...};
, the{...}
is like an adjective forX
, andX {...}
is thrown into something called struct-namespace, how many struct-namespace are there in a program? Is there only one specified bystruct
? - It seems like in C/C++ struct and class are almost the same? So in C++ struct and class are both in class-namespace?
c struct
add a comment |
I'm trying to understand this answer - typedef struct vs struct definitions [duplicate]:
(Line 3): ["] In the first line you are defining the identifier S within the struct name space (not in the C++ sense). [."]
- It seems like for
struct X {...};
, the{...}
is like an adjective forX
, andX {...}
is thrown into something called struct-namespace, how many struct-namespace are there in a program? Is there only one specified bystruct
? - It seems like in C/C++ struct and class are almost the same? So in C++ struct and class are both in class-namespace?
c struct
1
A c++ struct is simply a class that defaults to public.
– Goswin von Brederlow
Nov 23 '18 at 16:18
@GoswinvonBrederlow: Got it, thank you!
– BinaryTreeee
Nov 23 '18 at 16:34
add a comment |
I'm trying to understand this answer - typedef struct vs struct definitions [duplicate]:
(Line 3): ["] In the first line you are defining the identifier S within the struct name space (not in the C++ sense). [."]
- It seems like for
struct X {...};
, the{...}
is like an adjective forX
, andX {...}
is thrown into something called struct-namespace, how many struct-namespace are there in a program? Is there only one specified bystruct
? - It seems like in C/C++ struct and class are almost the same? So in C++ struct and class are both in class-namespace?
c struct
I'm trying to understand this answer - typedef struct vs struct definitions [duplicate]:
(Line 3): ["] In the first line you are defining the identifier S within the struct name space (not in the C++ sense). [."]
- It seems like for
struct X {...};
, the{...}
is like an adjective forX
, andX {...}
is thrown into something called struct-namespace, how many struct-namespace are there in a program? Is there only one specified bystruct
? - It seems like in C/C++ struct and class are almost the same? So in C++ struct and class are both in class-namespace?
c struct
c struct
edited Nov 23 '18 at 14:17
BinaryTreeee
asked Nov 23 '18 at 14:16
BinaryTreeeeBinaryTreeee
606424
606424
1
A c++ struct is simply a class that defaults to public.
– Goswin von Brederlow
Nov 23 '18 at 16:18
@GoswinvonBrederlow: Got it, thank you!
– BinaryTreeee
Nov 23 '18 at 16:34
add a comment |
1
A c++ struct is simply a class that defaults to public.
– Goswin von Brederlow
Nov 23 '18 at 16:18
@GoswinvonBrederlow: Got it, thank you!
– BinaryTreeee
Nov 23 '18 at 16:34
1
1
A c++ struct is simply a class that defaults to public.
– Goswin von Brederlow
Nov 23 '18 at 16:18
A c++ struct is simply a class that defaults to public.
– Goswin von Brederlow
Nov 23 '18 at 16:18
@GoswinvonBrederlow: Got it, thank you!
– BinaryTreeee
Nov 23 '18 at 16:34
@GoswinvonBrederlow: Got it, thank you!
– BinaryTreeee
Nov 23 '18 at 16:34
add a comment |
2 Answers
2
active
oldest
votes
1 (for C): there is one namespace for all structure tags. You can have as many different tags as you want, even if the same name is used in other namespaces.
Each structure on its own is its own namespace (there are "infinite" struct namespaces).
// You can have
struct foo { int var; }
struct bar { double var; }
struct baz { int var; }
struct qux { int var; }
// with no conflict between all the `var`
So each differentX
is a namespace?
– BinaryTreeee
Nov 23 '18 at 14:20
1
sort of, if you have astruct one
,one
is an identifier belonging to the struct namespace.one
can also be one or more identifier(s) in other namespaces.
– pmg
Nov 23 '18 at 14:27
OK, sostruct {...} one, two, three;
only one namespaces and three identifiers for it? And is that in this case the struct is anonymous?
– BinaryTreeee
Nov 23 '18 at 14:30
still not quite right. instruct {...} one, two, three;
there is no identifier for the struct namespace: it's a struct without a tag (an anonymous struct, using no namespace). The identifiersone
,two
, andthree
belong to the regular namespace.
– pmg
Nov 23 '18 at 14:33
1
Yes, the same meaning :)
– pmg
Nov 23 '18 at 14:41
|
show 5 more comments
From the C99 standard draft
6.2.3
6.2.3 Name spaces of identifiers
If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate namespaces
for various categories of identifiers, as follows:
—label names
(disambiguated by the syntax of the label declaration and use);
—the
tags
of structures, unions, and enumerations (disambiguated by following any
of the keywords
struct
,
union
,or
enum
);
—the
members
of structures or unions; each structure or union has a separate name
space for its members (disambiguated by the type of the expression used to access the
member via the
.
or
->
operator);
—all other identifiers, called
ordinary identifiers
(declared in ordinary declarators or as
enumeration constants).
In other words, if you have struct S
, then S
is in the struct namespace, whereas if you have int S
as well, then it's in the namespace of ordinary identifiers.
This is not the case for C++
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%2f53448315%2fsome-questions-about-struct-namespace-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 (for C): there is one namespace for all structure tags. You can have as many different tags as you want, even if the same name is used in other namespaces.
Each structure on its own is its own namespace (there are "infinite" struct namespaces).
// You can have
struct foo { int var; }
struct bar { double var; }
struct baz { int var; }
struct qux { int var; }
// with no conflict between all the `var`
So each differentX
is a namespace?
– BinaryTreeee
Nov 23 '18 at 14:20
1
sort of, if you have astruct one
,one
is an identifier belonging to the struct namespace.one
can also be one or more identifier(s) in other namespaces.
– pmg
Nov 23 '18 at 14:27
OK, sostruct {...} one, two, three;
only one namespaces and three identifiers for it? And is that in this case the struct is anonymous?
– BinaryTreeee
Nov 23 '18 at 14:30
still not quite right. instruct {...} one, two, three;
there is no identifier for the struct namespace: it's a struct without a tag (an anonymous struct, using no namespace). The identifiersone
,two
, andthree
belong to the regular namespace.
– pmg
Nov 23 '18 at 14:33
1
Yes, the same meaning :)
– pmg
Nov 23 '18 at 14:41
|
show 5 more comments
1 (for C): there is one namespace for all structure tags. You can have as many different tags as you want, even if the same name is used in other namespaces.
Each structure on its own is its own namespace (there are "infinite" struct namespaces).
// You can have
struct foo { int var; }
struct bar { double var; }
struct baz { int var; }
struct qux { int var; }
// with no conflict between all the `var`
So each differentX
is a namespace?
– BinaryTreeee
Nov 23 '18 at 14:20
1
sort of, if you have astruct one
,one
is an identifier belonging to the struct namespace.one
can also be one or more identifier(s) in other namespaces.
– pmg
Nov 23 '18 at 14:27
OK, sostruct {...} one, two, three;
only one namespaces and three identifiers for it? And is that in this case the struct is anonymous?
– BinaryTreeee
Nov 23 '18 at 14:30
still not quite right. instruct {...} one, two, three;
there is no identifier for the struct namespace: it's a struct without a tag (an anonymous struct, using no namespace). The identifiersone
,two
, andthree
belong to the regular namespace.
– pmg
Nov 23 '18 at 14:33
1
Yes, the same meaning :)
– pmg
Nov 23 '18 at 14:41
|
show 5 more comments
1 (for C): there is one namespace for all structure tags. You can have as many different tags as you want, even if the same name is used in other namespaces.
Each structure on its own is its own namespace (there are "infinite" struct namespaces).
// You can have
struct foo { int var; }
struct bar { double var; }
struct baz { int var; }
struct qux { int var; }
// with no conflict between all the `var`
1 (for C): there is one namespace for all structure tags. You can have as many different tags as you want, even if the same name is used in other namespaces.
Each structure on its own is its own namespace (there are "infinite" struct namespaces).
// You can have
struct foo { int var; }
struct bar { double var; }
struct baz { int var; }
struct qux { int var; }
// with no conflict between all the `var`
edited Nov 23 '18 at 14:40
answered Nov 23 '18 at 14:17
pmgpmg
84.8k9100171
84.8k9100171
So each differentX
is a namespace?
– BinaryTreeee
Nov 23 '18 at 14:20
1
sort of, if you have astruct one
,one
is an identifier belonging to the struct namespace.one
can also be one or more identifier(s) in other namespaces.
– pmg
Nov 23 '18 at 14:27
OK, sostruct {...} one, two, three;
only one namespaces and three identifiers for it? And is that in this case the struct is anonymous?
– BinaryTreeee
Nov 23 '18 at 14:30
still not quite right. instruct {...} one, two, three;
there is no identifier for the struct namespace: it's a struct without a tag (an anonymous struct, using no namespace). The identifiersone
,two
, andthree
belong to the regular namespace.
– pmg
Nov 23 '18 at 14:33
1
Yes, the same meaning :)
– pmg
Nov 23 '18 at 14:41
|
show 5 more comments
So each differentX
is a namespace?
– BinaryTreeee
Nov 23 '18 at 14:20
1
sort of, if you have astruct one
,one
is an identifier belonging to the struct namespace.one
can also be one or more identifier(s) in other namespaces.
– pmg
Nov 23 '18 at 14:27
OK, sostruct {...} one, two, three;
only one namespaces and three identifiers for it? And is that in this case the struct is anonymous?
– BinaryTreeee
Nov 23 '18 at 14:30
still not quite right. instruct {...} one, two, three;
there is no identifier for the struct namespace: it's a struct without a tag (an anonymous struct, using no namespace). The identifiersone
,two
, andthree
belong to the regular namespace.
– pmg
Nov 23 '18 at 14:33
1
Yes, the same meaning :)
– pmg
Nov 23 '18 at 14:41
So each different
X
is a namespace?– BinaryTreeee
Nov 23 '18 at 14:20
So each different
X
is a namespace?– BinaryTreeee
Nov 23 '18 at 14:20
1
1
sort of, if you have a
struct one
, one
is an identifier belonging to the struct namespace. one
can also be one or more identifier(s) in other namespaces.– pmg
Nov 23 '18 at 14:27
sort of, if you have a
struct one
, one
is an identifier belonging to the struct namespace. one
can also be one or more identifier(s) in other namespaces.– pmg
Nov 23 '18 at 14:27
OK, so
struct {...} one, two, three;
only one namespaces and three identifiers for it? And is that in this case the struct is anonymous?– BinaryTreeee
Nov 23 '18 at 14:30
OK, so
struct {...} one, two, three;
only one namespaces and three identifiers for it? And is that in this case the struct is anonymous?– BinaryTreeee
Nov 23 '18 at 14:30
still not quite right. in
struct {...} one, two, three;
there is no identifier for the struct namespace: it's a struct without a tag (an anonymous struct, using no namespace). The identifiers one
, two
, and three
belong to the regular namespace.– pmg
Nov 23 '18 at 14:33
still not quite right. in
struct {...} one, two, three;
there is no identifier for the struct namespace: it's a struct without a tag (an anonymous struct, using no namespace). The identifiers one
, two
, and three
belong to the regular namespace.– pmg
Nov 23 '18 at 14:33
1
1
Yes, the same meaning :)
– pmg
Nov 23 '18 at 14:41
Yes, the same meaning :)
– pmg
Nov 23 '18 at 14:41
|
show 5 more comments
From the C99 standard draft
6.2.3
6.2.3 Name spaces of identifiers
If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate namespaces
for various categories of identifiers, as follows:
—label names
(disambiguated by the syntax of the label declaration and use);
—the
tags
of structures, unions, and enumerations (disambiguated by following any
of the keywords
struct
,
union
,or
enum
);
—the
members
of structures or unions; each structure or union has a separate name
space for its members (disambiguated by the type of the expression used to access the
member via the
.
or
->
operator);
—all other identifiers, called
ordinary identifiers
(declared in ordinary declarators or as
enumeration constants).
In other words, if you have struct S
, then S
is in the struct namespace, whereas if you have int S
as well, then it's in the namespace of ordinary identifiers.
This is not the case for C++
add a comment |
From the C99 standard draft
6.2.3
6.2.3 Name spaces of identifiers
If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate namespaces
for various categories of identifiers, as follows:
—label names
(disambiguated by the syntax of the label declaration and use);
—the
tags
of structures, unions, and enumerations (disambiguated by following any
of the keywords
struct
,
union
,or
enum
);
—the
members
of structures or unions; each structure or union has a separate name
space for its members (disambiguated by the type of the expression used to access the
member via the
.
or
->
operator);
—all other identifiers, called
ordinary identifiers
(declared in ordinary declarators or as
enumeration constants).
In other words, if you have struct S
, then S
is in the struct namespace, whereas if you have int S
as well, then it's in the namespace of ordinary identifiers.
This is not the case for C++
add a comment |
From the C99 standard draft
6.2.3
6.2.3 Name spaces of identifiers
If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate namespaces
for various categories of identifiers, as follows:
—label names
(disambiguated by the syntax of the label declaration and use);
—the
tags
of structures, unions, and enumerations (disambiguated by following any
of the keywords
struct
,
union
,or
enum
);
—the
members
of structures or unions; each structure or union has a separate name
space for its members (disambiguated by the type of the expression used to access the
member via the
.
or
->
operator);
—all other identifiers, called
ordinary identifiers
(declared in ordinary declarators or as
enumeration constants).
In other words, if you have struct S
, then S
is in the struct namespace, whereas if you have int S
as well, then it's in the namespace of ordinary identifiers.
This is not the case for C++
From the C99 standard draft
6.2.3
6.2.3 Name spaces of identifiers
If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate namespaces
for various categories of identifiers, as follows:
—label names
(disambiguated by the syntax of the label declaration and use);
—the
tags
of structures, unions, and enumerations (disambiguated by following any
of the keywords
struct
,
union
,or
enum
);
—the
members
of structures or unions; each structure or union has a separate name
space for its members (disambiguated by the type of the expression used to access the
member via the
.
or
->
operator);
—all other identifiers, called
ordinary identifiers
(declared in ordinary declarators or as
enumeration constants).
In other words, if you have struct S
, then S
is in the struct namespace, whereas if you have int S
as well, then it's in the namespace of ordinary identifiers.
This is not the case for C++
answered Nov 23 '18 at 14:29
JETMJETM
2,15441731
2,15441731
add a comment |
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.
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%2f53448315%2fsome-questions-about-struct-namespace-in-c%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
1
A c++ struct is simply a class that defaults to public.
– Goswin von Brederlow
Nov 23 '18 at 16:18
@GoswinvonBrederlow: Got it, thank you!
– BinaryTreeee
Nov 23 '18 at 16:34