The Type Struct
The Type Struct
Structure(Struct)
What’s a struct
• An array is a variable used to group data that are all of the
same type.
• The structure type allows to group several data:
• Same type
• Different types
within a single variable, called record. Each element of the structure
is called a member of this structure.
• In C programming, a struct (or structure) type is a collection of
variables (can be of different types) under a single name.
How to declare and create a struct
• You can create a structure by using the struct keyword and
declare each of its members inside curly braces:
Output
What About Strings in Structures?
• Remember that strings in C are actually an array of
characters, and unfortunately, we can't assign directly a value
to an array like this:
Output
What About Strings in Structures?
• we can also assign values to members of a structure variable at
declaration time, in a single line. Just insert the values in a comma-
separated list inside curly braces {}.
• We don't have to use the strcpy() function for string values with this
technique:
Output
• Note: The order of the inserted values must match the order of the
variable types declared in the structure (13 for int, 'B' for char, etc).
How to Copy Structures?
• we can assign one structure to another.
• In the following example, the values of s1 are copied to s2:
Output
Modify Values
• If we want to change/modify a value, you can use the dot
syntax (.). And to modify a string value, the strcpy() function is
useful again:
Output
Example
Output