0% found this document useful (0 votes)
23 views

The Type Struct

The document discusses structures in C programming, including how to declare and create a struct, access struct members, assign values to struct members including strings, copy one struct to another, and modify struct member values.

Uploaded by

Bochra Messaoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

The Type Struct

The document discusses structures in C programming, including how to declare and create a struct, access struct members, assign values to struct members including strings, copy one struct to another, and modify struct member values.

Uploaded by

Bochra Messaoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

The type

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:

• To access the structure, you must create a variable of it.


• Use the struct keyword inside the main() method, followed by
the name of the structure and then the name of the structure
variable:
How to access a struct members
• To access members of a structure, use the dot syntax (.):

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:

An error will occur


What About Strings in Structures?
• As is a solution, we can use the strcpy() function and assign
the value to s1.Str, 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

You might also like