The algorithm for C initialization as described in the standard and implemented in gcc allow this.
struct X{
int a,b,c;
};
int main()
{
struct X x = {.a=1,2,3,.a=1, 2, 3};
}
https://godbolt.org/z/7naedbEM6
Basically, when a designed initializer is found the "cursor" goes to
that member and the following members are initialized in order.
On 2024-10-13, Thiago Adams <thiago.adams@gmail.com> wrote:
The algorithm for C initialization as described in the standard
and implemented in gcc allow this.
struct X{
int a,b,c;
};
int main()
{
struct X x = {.a=1,2,3,.a=1, 2, 3};
}
https://godbolt.org/z/7naedbEM6
Basically, when a designed initializer is found the "cursor" goes
to that member and the following members are initialized in
order.
I do not suspect that therw is an observable order. I.e. as
in a required order considered observable behavior.
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2024-10-13, Thiago Adams <thiago.adams@gmail.com> wrote:
The algorithm for C initialization as described in the standard
and implemented in gcc allow this.
struct X{
int a,b,c;
};
int main()
{
struct X x = {.a=1,2,3,.a=1, 2, 3};
}
https://godbolt.org/z/7naedbEM6
Basically, when a designed initializer is found the "cursor" goes
to that member and the following members are initialized in
order.
I do not suspect that therw is an observable order. I.e. as
in a required order considered observable behavior.
Have you checked to C standard to see what it says about that?
On 14/10/2024 02:00, Tim Rentsch wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2024-10-13, Thiago Adams <thiago.adams@gmail.com> wrote:
The algorithm for C initialization as described in the standard
and implemented in gcc allow this.
struct X{
int a,b,c;
};
int main()
{
struct X x = {.a=1,2,3,.a=1, 2, 3};
}
https://godbolt.org/z/7naedbEM6
Basically, when a designed initializer is found the "cursor" goes
to that member and the following members are initialized in
order.
I do not suspect that therw is an observable order. I.e. as
in a required order considered observable behavior.
Have you checked to C standard to see what it says about that?
On 14/10/2024 08:14, Thiago Adams wrote:
On 14/10/2024 02:00, Tim Rentsch wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2024-10-13, Thiago Adams <thiago.adams@gmail.com> wrote:
The algorithm for C initialization as described in the standard
and implemented in gcc allow this.
struct X{
int a,b,c;
};
int main()
{
struct X x = {.a=1,2,3,.a=1, 2, 3};
}
https://godbolt.org/z/7naedbEM6
Basically, when a designed initializer is found the "cursor" goes
to that member and the following members are initialized in
order.
I do not suspect that therw is an observable order. I.e. as
in a required order considered observable behavior.
Have you checked to C standard to see what it says about that?
I'm not only interested in this part, but since it raised some doubts, I decided to do some checks
#include <stdio.h>
struct X {
int i;
};
int f() {
printf("f");
return 1;
}
int main() {
struct X x = {.i = f(), .i = f() };
}
https://godbolt.org/z/rf984cGMM
There is a warning in this sample and f is called just once.
int main() {Forgot to say that I didn't find this part on the standard.
struct X x = {.i = f(), .i = f() };
}
https://godbolt.org/z/rf984cGMM
There is a warning in this sample and f is called just once.
I think initialization is also very superficial on books.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 991 |
Nodes: | 10 (0 / 10) |
Uptime: | 120:12:32 |
Calls: | 12,958 |
Files: | 186,574 |
Messages: | 3,265,650 |