Structs
Structs
Intro
Structs, or structures, give a way of implementing complex data structures in c.
They are collections of different variable types.
The variables inside a struct are called “members”
Members exist in their own namespace and have their own scope.
Unlike object oriented languages, c does not allow structures to directly contain their own methods.
Default way to access: “struct name.member = …“
for a ptr to a struct you can access members of structure pointed to using “->”
Example
Typedef
It is often useful to construct type definitions.
Typedef can do this so you don’t have to type struct over and over.
Memory Layout for structs
Address
0x200
0x319
Value
“Jean\0”
Name
0x320
0x439
“Valjean\0”
0x440
0x441
0x442
0x443
24601
name_first
name_last
number
Functions and structs
Variables in c are passed by creating a copy.
For this reason, c defines that structs are also passed by copy.
This means that arrays that are member of a structure are copied.
Pass by copy can be very wasteful and so it is usually recommended to pass by reference via a pointer to the struct.
/docProps/thumbnail.jpeg