Академический Документы
Профессиональный Документы
Культура Документы
Introduction to Programming
CSC 1023
Introduction to Programming
Chapter 8:
Structures & Pointers
CSC 1023
Introduction to Programming
Overview
• Structures
Accessing structure pointer
A sample program using structures
• Pointers
CSC 1023
Introduction to Programming
Structures
• A structure is a collection of variables
referenced under one name, providing a
convenient means of keeping related
information together.
• A structure declaration forms a template that
may be used to create structure objects (that
is instances of a structure).
• The variables that make up the structure are
called members.
CSC 1023
Introduction to Programming
Structures
• Structure members are also referred to as
elements or fields.
• All of the members of a structure are logically
related.
• The key word struct tells the compiler that a
structure is being declared.
CSC 1023
Introduction to Programming
Structures
• Example:
struct address
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
};
CSC 1023
Introduction to Programming
Structures
• At this point no variable is being created and
only the form of the data has been defined.
• When the structure is defined, a compound
variable type is defined but not the variable.
CSC 1023
Introduction to Programming
Structures
• One or more structure variables may be created while
declaring a structure.
struct address
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info, binfo, cinfo;
• This defines a structure type address and declares
variables addr_info, binfoand cinfo of that type.
CSC 1023
Introduction to Programming
Structures
• The general form of a structure declaration is,
struct struct_type_name{
type member_name;
type member_name;
type member_name;
. . .
. . .
} structure_variables;
CSC 1023
Introduction to Programming
• For example:
address. zip = 71700;
structure-name.member_name
CSC 1023
Introduction to Programming
Pointers
• In general, a variable directly contains a
specific values where as a pointer contains
the address of a variable that contains the
specific value.
• Pointers indirectly references the value.
• Referencing a value through a pointer is
called as indirection.
• Pointers must be declared before they can be
used.
CSC 1023
Introduction to Programming
Pointers
• Example:
int x, *y
Pointers
• Example:
int y = 5;
int *x;
x = &y;
• In this case, the address of the variable y to pointer
variable x will be stored in x.
CSC 1023
Introduction to Programming
Pointers
• Example:
• Lom siap!!!
CSC 1023
Introduction to Programming
Pointers
• The * operator is referred to as the indirection
operator or de-referencing operator.
• The * operator returns a synonym for the object to
which its pointer operand points. This way of using *
operator is called as de-referencing a pointer.
• De-referenced pointer may also be used to receive an
input value as
cin >> *xptr;
CSC 1023
Introduction to Programming
Pointers
• C++ statement Output
Pointers
• Example 1 and 2 refer to Figure 2 and
Figure 3 (Ms Word slide)
CSC 1023
Introduction to Programming