Sei sulla pagina 1di 26

CHAPTER 7

Pointer

Outline
Introduction
What is memory address and how to access?
Example 1

What are pointers?


How to use pointers?
Example 2

What are the differences?


Example 3

Pointer Arithmetic
Example 4
Example 5

Outline
Array of pointer
Example 6
Example 7

Pointer to pointer
Example 8

Calling function by reference


Pass arguments by reference
Example 9

Call-by-value vs Call-by-reference
???

Introduction
Some C programming tasks are performed

more easily with pointers.


Other tasks cannot be performed without
using pointers.
Such as dynamic memory allocation.

What is memory address and how


to access?
Every variable is a memory location.
Every memory location has its address

defined.
Can be accessed using ampersand (&)
operator.
Denotes as an address in memory.

Example 1

What are pointers?


A variable whose value is the address of

another variable.
For example: direct address of the memory
location.
General form of a pointer variable
declaration is:
type *variable_name
type =
pointers base
type

variable_name = name
of the pointer variable

What are the pointers?


The asterisk * used to declare a pointer.
Same asterisk that use for multiplication.
However, in this statement the asterisk is

being used to designate a variable as a


pointer.
Following are the valid pointer declaration:

How to use pointers?


Step 1: Define a pointer variable.
Step 2: Assign the address of a variable to

a pointer.
Step 3: Access the value at the address
available in the pointer variable.

Example 2

What are the difference?


variable_number refer to the value of the

variable.
&variable_number refer to the address
that contains value of the variable.
*variable_pointer refer to the value inside

the address of pointer.


variable_pointer refer to the address of
pointer.

Example 3

Pointer Arithmetic
Arithmetic operation can be performed to

the pointer.
For example: pointer++;
After above operation, the pointer will
point to the location 1004.
Because each time pointer incremented, it
will point to the next integer location.
Next integer location is 4 bytes next to the
current location.
Decrementing a pointer also can be done.

Example 4 Incrementing a pointer

Pointer Arithmetic
Pointer can be compared by using

relational operators: ==, <, and >.


If pointer1 and pointer2 point to variables
that are related to each other, then
pointer1 and pointer2 can be
meaningfully compared.

Example 5 Pointer
comparison

Array of Pointer
There may be a situation when we want to

maintain an array which can store pointers


to an integer character or any other data
type available.

Example 6 Recap of array

Example 7 Array of integer


pointer

Pointer to pointer
A pointer to a pointer is a form of multiple

indirection or a chain of pointers.


Normally, pointer contains the address of
variable.
Pointer to pointer contains the address of
the second pointer.
Declaration: int **pointer

Example 8

Calling functions by
reference
Call by reference with pointer arguments
Passes address of argument using &

operator.
Allows you to change actual location in
memory.
Arrays are not passed with & because the
array name is already a pointer.
* operator
Used as alias or nickname for variable inside

of function.
void funtion1 (int *number)

Passing arguments to function by


reference
Two ways to pass arguments to a function:
Call-by-value
Call-by-reference

Call-by-value is normal pass arguments.


However, you can pass using pointer (Call-

by-reference).
This is accomplished by applying the
address operator (&) to the variable.

Example 9

Pass-by-value vs pass-by-reference
Passing argument by value

Passing argument by reference

-using ordinary variable

-using pointer variable

-using regular variable to pass value

- using pointer to pass value

-uses more memory space

-save some memory space

-copy of actual variable/parameter is


passed to the function

-memory address of the


variable/parameter is passed to the
function

-any modification to variable/parameter -any modification to variable/parameter


inside function will not affect actual
inside function will affect actual value
value

POP-QUIZ
int value1, value2, *ptr1, *ptr2;
WHAT IS THE
int a[5]={5,4,3,2,1};
value1=10;
OUTPUT FOR
value2=20;
LINE X & LINE
ptr1=&value1;
ptr2=&value2;
*ptr2=*ptr1+2;
ptr2=ptr1;
printf("%d %d %d
LINE X
\n",value2,*ptr1,*ptr2);
ptr1=&a[1];
LINE Y
ptr2=ptr1++;
printf("%d %d %d
\n",*ptr1,*(ptr1+2),*ptr2);

Y?

Potrebbero piacerti anche