Вы находитесь на странице: 1из 19

CSC 1023

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

Accessing structure members


• Individual members of a structure are accessed
through the use of the . (dot) operator.

• For example:
 
address. zip = 71700;

• The general format is,

structure-name.member_name
CSC 1023
Introduction to Programming

Accessing structure members


• To print the name,

cout <<” The name of the Person


is “<< address.name;
CSC 1023
Introduction to Programming

A sample program using Structures


• Refer to Figure 1(Ms Word slide)
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

• In this example, x is an integer variable and * is the pointer to


the integer variable.
• Each variable declared as a pointer must be preceded by an
asterisk (*).
• Pointers may be initialized to 0, NULL or an address.
• A pointer with 0 or NULL points to nothing.

• “&” is called as an address operator which returns the


memory address of its operand.
CSC 1023
Introduction to Programming

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

cout <<*x << endl; 5


 
cout <<y <<endl; 5
 
*x = 7; assigns the value 7 to x
CSC 1023
Introduction to Programming

Pointers
• Example 1 and 2 refer to Figure 2 and
Figure 3 (Ms Word slide)
CSC 1023
Introduction to Programming

Calling functions by reference


• Example 1 :Refer to Figure 4(Ms Word slide)

Вам также может понравиться