C - Creating an anonymous struct instance
In this code, a structure is defined as follows:
typedef struct
{
int line;
int column;
} Pos;
And later used this way:
Pos get_pos ( int delta )
{
...
return ( Pos ){ f->line, f->column + delta };
}
The line, return ( Pos ){ f->line, f->column + delta }
seems to be creating an anonymous instance of the struct Pos
with initialized values. What is this technique called and how does it work? Where can I learn more about it?
c struct compound-literals
add a comment |
In this code, a structure is defined as follows:
typedef struct
{
int line;
int column;
} Pos;
And later used this way:
Pos get_pos ( int delta )
{
...
return ( Pos ){ f->line, f->column + delta };
}
The line, return ( Pos ){ f->line, f->column + delta }
seems to be creating an anonymous instance of the struct Pos
with initialized values. What is this technique called and how does it work? Where can I learn more about it?
c struct compound-literals
7
"Compound literal", since C99.
– Amadan
Feb 8 at 4:20
4
C11 Standard - 6.5.2.5 Compound literals
– David C. Rankin
Feb 8 at 4:24
stackoverflow.com/questions/tagged/… . There doesn't seem to be a canonical question, maybe this could be it
– M.M
Feb 8 at 4:25
Thanks! Updated the question tag accordingly.
– Jet Blue
Feb 8 at 7:18
add a comment |
In this code, a structure is defined as follows:
typedef struct
{
int line;
int column;
} Pos;
And later used this way:
Pos get_pos ( int delta )
{
...
return ( Pos ){ f->line, f->column + delta };
}
The line, return ( Pos ){ f->line, f->column + delta }
seems to be creating an anonymous instance of the struct Pos
with initialized values. What is this technique called and how does it work? Where can I learn more about it?
c struct compound-literals
In this code, a structure is defined as follows:
typedef struct
{
int line;
int column;
} Pos;
And later used this way:
Pos get_pos ( int delta )
{
...
return ( Pos ){ f->line, f->column + delta };
}
The line, return ( Pos ){ f->line, f->column + delta }
seems to be creating an anonymous instance of the struct Pos
with initialized values. What is this technique called and how does it work? Where can I learn more about it?
c struct compound-literals
c struct compound-literals
edited Feb 8 at 7:12
Jet Blue
asked Feb 8 at 4:18
Jet BlueJet Blue
2,1821825
2,1821825
7
"Compound literal", since C99.
– Amadan
Feb 8 at 4:20
4
C11 Standard - 6.5.2.5 Compound literals
– David C. Rankin
Feb 8 at 4:24
stackoverflow.com/questions/tagged/… . There doesn't seem to be a canonical question, maybe this could be it
– M.M
Feb 8 at 4:25
Thanks! Updated the question tag accordingly.
– Jet Blue
Feb 8 at 7:18
add a comment |
7
"Compound literal", since C99.
– Amadan
Feb 8 at 4:20
4
C11 Standard - 6.5.2.5 Compound literals
– David C. Rankin
Feb 8 at 4:24
stackoverflow.com/questions/tagged/… . There doesn't seem to be a canonical question, maybe this could be it
– M.M
Feb 8 at 4:25
Thanks! Updated the question tag accordingly.
– Jet Blue
Feb 8 at 7:18
7
7
"Compound literal", since C99.
– Amadan
Feb 8 at 4:20
"Compound literal", since C99.
– Amadan
Feb 8 at 4:20
4
4
C11 Standard - 6.5.2.5 Compound literals
– David C. Rankin
Feb 8 at 4:24
C11 Standard - 6.5.2.5 Compound literals
– David C. Rankin
Feb 8 at 4:24
stackoverflow.com/questions/tagged/… . There doesn't seem to be a canonical question, maybe this could be it
– M.M
Feb 8 at 4:25
stackoverflow.com/questions/tagged/… . There doesn't seem to be a canonical question, maybe this could be it
– M.M
Feb 8 at 4:25
Thanks! Updated the question tag accordingly.
– Jet Blue
Feb 8 at 7:18
Thanks! Updated the question tag accordingly.
– Jet Blue
Feb 8 at 7:18
add a comment |
1 Answer
1
active
oldest
votes
This is called a compound literal, and is documented in section 6.5.2.5 of the C standard.
An excerpt of this section is as follows:
3 A postfix expression that consists of a parenthesized type name followed by a brace- enclosed list of initializers is a
compound literal. It provides an unnamed object whose value is
given by the initializer list.
4 If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in
6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an
object type), the type of the compound literal is that specified by
the type name. In either case, the result is an lvalue.
5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound
literal occurs outside the body of a function, the object has
static storage duration; otherwise, it has automatic storage
duration associated with the enclosing block.
In your case the compound literal is for a struct
, but they can be created for arrays as well. Paragraph 8 gives an example:
8 EXAMPLE 1 The file scope definition
int *p = (int ){2, 4};
initializes
p
to point to the first element of an array of
two ints, the first having the value two and the second,
four. The expressions in this compound literal are required
to be constant. The unnamed object has static storage duration.
Note also that a compound literal is an lvalue, which means you can take its address:
Pos *p = &( Pos ){ f->line, f->column + delta };
This object has a lifetime associated with its scope, meaning that once the scope ends the object no longer exists. So don't carry around its address after it goes out of scope.
You can also use a compound literal with a designated initializer:
return ( Pos ){ .line=f->line, .column=f->column + delta };
So it applies to all type (not only aggregates)?
– Some Name
Feb 8 at 7:01
1
@SomeName As it turns out, yes.int x = (int){4};
compiles, although there's not much use in using it for basic types.
– dbush
Feb 8 at 12:59
add a comment |
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%2f54585928%2fc-creating-an-anonymous-struct-instance%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
This is called a compound literal, and is documented in section 6.5.2.5 of the C standard.
An excerpt of this section is as follows:
3 A postfix expression that consists of a parenthesized type name followed by a brace- enclosed list of initializers is a
compound literal. It provides an unnamed object whose value is
given by the initializer list.
4 If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in
6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an
object type), the type of the compound literal is that specified by
the type name. In either case, the result is an lvalue.
5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound
literal occurs outside the body of a function, the object has
static storage duration; otherwise, it has automatic storage
duration associated with the enclosing block.
In your case the compound literal is for a struct
, but they can be created for arrays as well. Paragraph 8 gives an example:
8 EXAMPLE 1 The file scope definition
int *p = (int ){2, 4};
initializes
p
to point to the first element of an array of
two ints, the first having the value two and the second,
four. The expressions in this compound literal are required
to be constant. The unnamed object has static storage duration.
Note also that a compound literal is an lvalue, which means you can take its address:
Pos *p = &( Pos ){ f->line, f->column + delta };
This object has a lifetime associated with its scope, meaning that once the scope ends the object no longer exists. So don't carry around its address after it goes out of scope.
You can also use a compound literal with a designated initializer:
return ( Pos ){ .line=f->line, .column=f->column + delta };
So it applies to all type (not only aggregates)?
– Some Name
Feb 8 at 7:01
1
@SomeName As it turns out, yes.int x = (int){4};
compiles, although there's not much use in using it for basic types.
– dbush
Feb 8 at 12:59
add a comment |
This is called a compound literal, and is documented in section 6.5.2.5 of the C standard.
An excerpt of this section is as follows:
3 A postfix expression that consists of a parenthesized type name followed by a brace- enclosed list of initializers is a
compound literal. It provides an unnamed object whose value is
given by the initializer list.
4 If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in
6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an
object type), the type of the compound literal is that specified by
the type name. In either case, the result is an lvalue.
5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound
literal occurs outside the body of a function, the object has
static storage duration; otherwise, it has automatic storage
duration associated with the enclosing block.
In your case the compound literal is for a struct
, but they can be created for arrays as well. Paragraph 8 gives an example:
8 EXAMPLE 1 The file scope definition
int *p = (int ){2, 4};
initializes
p
to point to the first element of an array of
two ints, the first having the value two and the second,
four. The expressions in this compound literal are required
to be constant. The unnamed object has static storage duration.
Note also that a compound literal is an lvalue, which means you can take its address:
Pos *p = &( Pos ){ f->line, f->column + delta };
This object has a lifetime associated with its scope, meaning that once the scope ends the object no longer exists. So don't carry around its address after it goes out of scope.
You can also use a compound literal with a designated initializer:
return ( Pos ){ .line=f->line, .column=f->column + delta };
So it applies to all type (not only aggregates)?
– Some Name
Feb 8 at 7:01
1
@SomeName As it turns out, yes.int x = (int){4};
compiles, although there's not much use in using it for basic types.
– dbush
Feb 8 at 12:59
add a comment |
This is called a compound literal, and is documented in section 6.5.2.5 of the C standard.
An excerpt of this section is as follows:
3 A postfix expression that consists of a parenthesized type name followed by a brace- enclosed list of initializers is a
compound literal. It provides an unnamed object whose value is
given by the initializer list.
4 If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in
6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an
object type), the type of the compound literal is that specified by
the type name. In either case, the result is an lvalue.
5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound
literal occurs outside the body of a function, the object has
static storage duration; otherwise, it has automatic storage
duration associated with the enclosing block.
In your case the compound literal is for a struct
, but they can be created for arrays as well. Paragraph 8 gives an example:
8 EXAMPLE 1 The file scope definition
int *p = (int ){2, 4};
initializes
p
to point to the first element of an array of
two ints, the first having the value two and the second,
four. The expressions in this compound literal are required
to be constant. The unnamed object has static storage duration.
Note also that a compound literal is an lvalue, which means you can take its address:
Pos *p = &( Pos ){ f->line, f->column + delta };
This object has a lifetime associated with its scope, meaning that once the scope ends the object no longer exists. So don't carry around its address after it goes out of scope.
You can also use a compound literal with a designated initializer:
return ( Pos ){ .line=f->line, .column=f->column + delta };
This is called a compound literal, and is documented in section 6.5.2.5 of the C standard.
An excerpt of this section is as follows:
3 A postfix expression that consists of a parenthesized type name followed by a brace- enclosed list of initializers is a
compound literal. It provides an unnamed object whose value is
given by the initializer list.
4 If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in
6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an
object type), the type of the compound literal is that specified by
the type name. In either case, the result is an lvalue.
5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound
literal occurs outside the body of a function, the object has
static storage duration; otherwise, it has automatic storage
duration associated with the enclosing block.
In your case the compound literal is for a struct
, but they can be created for arrays as well. Paragraph 8 gives an example:
8 EXAMPLE 1 The file scope definition
int *p = (int ){2, 4};
initializes
p
to point to the first element of an array of
two ints, the first having the value two and the second,
four. The expressions in this compound literal are required
to be constant. The unnamed object has static storage duration.
Note also that a compound literal is an lvalue, which means you can take its address:
Pos *p = &( Pos ){ f->line, f->column + delta };
This object has a lifetime associated with its scope, meaning that once the scope ends the object no longer exists. So don't carry around its address after it goes out of scope.
You can also use a compound literal with a designated initializer:
return ( Pos ){ .line=f->line, .column=f->column + delta };
edited Feb 8 at 18:40
answered Feb 8 at 4:26
dbushdbush
103k13108145
103k13108145
So it applies to all type (not only aggregates)?
– Some Name
Feb 8 at 7:01
1
@SomeName As it turns out, yes.int x = (int){4};
compiles, although there's not much use in using it for basic types.
– dbush
Feb 8 at 12:59
add a comment |
So it applies to all type (not only aggregates)?
– Some Name
Feb 8 at 7:01
1
@SomeName As it turns out, yes.int x = (int){4};
compiles, although there's not much use in using it for basic types.
– dbush
Feb 8 at 12:59
So it applies to all type (not only aggregates)?
– Some Name
Feb 8 at 7:01
So it applies to all type (not only aggregates)?
– Some Name
Feb 8 at 7:01
1
1
@SomeName As it turns out, yes.
int x = (int){4};
compiles, although there's not much use in using it for basic types.– dbush
Feb 8 at 12:59
@SomeName As it turns out, yes.
int x = (int){4};
compiles, although there's not much use in using it for basic types.– dbush
Feb 8 at 12:59
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%2f54585928%2fc-creating-an-anonymous-struct-instance%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
7
"Compound literal", since C99.
– Amadan
Feb 8 at 4:20
4
C11 Standard - 6.5.2.5 Compound literals
– David C. Rankin
Feb 8 at 4:24
stackoverflow.com/questions/tagged/… . There doesn't seem to be a canonical question, maybe this could be it
– M.M
Feb 8 at 4:25
Thanks! Updated the question tag accordingly.
– Jet Blue
Feb 8 at 7:18