In computing, Array of Structures (AoS), Structure of Arrays (SoA) and Array of Structures of. Structure of arrays (or SoA) is a layout separating elements of a record (or 'struct' in the C programming language) into one parallel array per field.
Active6 years ago
This question already has an answer here:
- How to initialize a struct in accordance with C programming language standards 14 answers
Here's initialization I just found in somebody else's question.
I never saw something like this before and can't find explanation how is .name possible to be correct.
What I'm looking for is how step by step this process goes.
What I'm looking for is how step by step this process goes.
It looks like it gets:
Or am I totally wrong?
Yu Hao101k2222 gold badges178178 silver badges231231 bronze badges
zuberguzubergu2,80433 gold badges2020 silver badges3232 bronze badges
marked as duplicate by Jonathan Leffler cJun 18 '14 at 14:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
6 Answers
There are only two syntaxes at play here.
- Plain old array initialisation:
- A designated initialiser. See the accepted answer to this question: How to initialize a struct in accordance with C programming language standardsThe syntax is pretty self-explanatory though. You can initialise like this:or to use any ordering,
Community♦
DaveDave
27k55 gold badges4242 silver badges8686 bronze badges
my_data
is a struct with name
as a field and data[]
is arry of structs, you are initializing each index. read following: 5.20 Designated Initializers:
In a structure initializer, specify the name of a field to initialize with
.fieldname ='
before the element value. For example, given the following structure,the following initialization
is equivalent to
Another syntax which has the same meaning, obsolete since GCC 2.5, is
fieldname:'
, as shown here:You can also write:
as:
Second form may be convenient as you don't need to write in order for example above is equivalent to:
To understand array initialization read Strange initializer expression?
Additionally, you may also like to read @Shafik Yaghmour's answer for switch case: What is “…” in switch-case in C code
Additionally, you may also like to read @Shafik Yaghmour's answer for switch case: What is “…” in switch-case in C code
Community♦
Grijesh ChauhanGrijesh Chauhan47.3k1414 gold badges105105 silver badges167167 bronze badges
There's no 'step-by-step' here. When initialization is performed with constant expressions, the process is essentially performed at compile time. Of course, if the array is declared as a local object, it is allocated locally and initialized at run-time, but that can be still thought of as a single-step process that cannot be meaningfully subdivided.
Designated initializers allow you to supply an initializer for a specific member of struct object (or a specific element of an array). All other members get zero-initialized. So, if
my_data
is declared asthen your
is simply a more compact form of
I hope you know what the latter does.
AnTAnT268k3636 gold badges435435 silver badges683683 bronze badges
It's called designated initializer which is introduced in C99. It's used to initialize
struct
or arrays, in this example, struct
.Given
the following initialization
is equivalent to the C89-style
Yu HaoYu Hao101k2222 gold badges178178 silver badges231231 bronze badges
This is quite simple:
my_data
is a before defined structure type.So you want to declare an my_data
-array of some elements, as you would do with So the array would have 4 elements and you initialise them as
This is called a designated initializer (as i remember right).
and it just indicates that data has to be of type
dheindheinmy_dat
and has to be an array that needs to store so many my_data structures that there is a structure with each type member name Peter, James, John and Mike.4,20944 gold badges2828 silver badges6363 bronze badges
It's a designated initializer, introduced with the C99 standard; it allows you to initialize specific members of a struct or union object by name.
John BodeJohn Bodemy_data
is obviously a typedef for a struct
type that has a member name
of type char *
or char [N]
. 88.2k1515 gold badges8888 silver badges155155 bronze badges
Not the answer you're looking for? Browse other questions tagged carraysstructinitialization or ask your own question.
Active6 years ago
i am bit new to structs in c#..
My question says:
Write a console application that receives the following information for a set of students: studentid, studentname, coursename, date-of-birth.. The application should also be able to display the information being entered.. Implement this using structs..
I have come up till this-->
Please help me after this..
dtb178k2929 gold badges358358 silver badges408408 bronze badges
UnhandledExceptionUnhandledException
closed as unclear what you're asking by Hans Passant, MarcinJuraszek, bensiu, Sahil Mittal, Mark BellSep 13 '13 at 6:59
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2 Answers
You've started right - now you just need to fill the each
student
structure in the array:Now, just iterate once again and write these information to the console. I will let you do that, and I will let you try to make program to take any number of students, and not just 4.
Nemanja BoricNemanja Boric18.4k44 gold badges5353 silver badges7777 bronze badges
Given an instance of the struct, you set the values.
Note this code is incredibly fragile, since we aren't checking the input from the user at all. And you aren't clear to the user that you expect each data point to be entered on a separate line.
Bill GreggBill Gregg6,35311 gold badge1212 silver badges3030 bronze badges