Sei sulla pagina 1di 18

Structures

Tennov Simanjuntak

9/9/2016 THS 1
Definition
● A structure is a collection of one or more variables, possibly of
different types, grouped together under a single name for
convenient handling.
● It is a data storage type designed by you, the programmer, to
suit your programming needs exactly.
● Structures help to organize complicated data, particularly in
large programs, because they permit a group of related
variables to be treated as a unit instead of as separate entities.
● One traditional example of a structure is the payroll record: an
employee is described by a set of attributes such as name,
address, social security number, salary, etc.
● Another example, more typical for C, comes from graphics: a
point is a pair of coordinate, a rectangle is a pair of points, and
so on.
9/9/2016 THS 2
Basic of structures
● Structure example: a point in Cartesian's coordinate.

struct point{
int x;
int y;

};
9/9/2016 THS 3
Basic of structures
● struct definition:
struct point{
int x;
int y;
};
● keyword struct introduces a structure declaration.
● struct is enclosed in braces.
● Structure name follows the struct keyword. In the above
example structure name is point.
● The variables named in a structure are called members. In the
above examples: x and y.

9/9/2016 THS 4
Basic of structures
● struct variable declaration
There are 2 ways to declare structure:
– To follow the structure definition with a list of variables
struct point{
int x;
int y;
} first, second;
– Declare structure variables after definition in other location in
the source code:
struct point{
int x;
int y;
} first, second;
9/9/2016
/* Additional statements may
THS
be here */ 5
struct point first, second;
Accessing Members of a Structure

● Structuremembers are accessed using the structure member


operator (.), also called the dot operator, between the structure
name and the member name.
● Continuing from previous struct point example, we can set the
value of variables members as follow:
first.x = 50;
first.y = 100;
● To print:
printf(“%d,%d”, first.x, first.y);

9/9/2016 THS 6
Accessing Members of a Structure

● One major advantage using structure is that you can copy


information between structures of the same type with a
simple equation statement. From previous example:
second = first;
i.e.
second.x = first.x;
second.y = first.y;

9/9/2016 THS 7
9/9/2016 THS 8
Structures inside structure
● You can define a more complex structure that takes other
structures as its members. Example using struct point.

9/9/2016 THS 9
Structure and functions
● Legal operations on structure:
➢ Copy structure or assigning structure to structure, both have the
same structure type
➢ Accessing structure members
➢ Accessing structure address using address of operator (&)
● You can use function to assign structure to structure and to
access structure members.
● Further, a function may take structure as its parameter and also
returning a structure.

9/9/2016 THS 10
9/9/2016 THS 11
Pointer to structure
● C allows pointers to structure as it allows pointers to other type of
object (variable and function).
● Example:
struct point{
int x;
int y;
};
struct point coord, *pointer;
pointer = & coord;

● All pointers operation allow to other C' object such as variable is


also application to pointer to structure.

9/9/2016 THS 12
Pointer to structure:
accessing structure's member from pointer

9/9/2016 THS 13
Pointer to structure:
accessing structure's member from pointer

9/9/2016 THS 14
Array of structures
● As with other variable types, C allows using array of structures.
● You can declare array of structures using static or dynamic array
allocation.
struct student{
int nim;
char *name;
char *prodi;
};
● Static array allocation:
struct student lst_student [30];
● Dynamic array allocation:
struct student **lst_stu;
//malloc statements for multi array

9/9/2016 THS 15
9/9/2016 THS 16
9/9/2016 THS 17
Any Question ?

9/9/2016 THS 18

Potrebbero piacerti anche