Escolar Documentos
Profissional Documentos
Cultura Documentos
Pointer
Outline
Introduction
What is memory address and how to access?
Example 1
Pointer Arithmetic
Example 4
Example 5
Outline
Array of pointer
Example 6
Example 7
Pointer to pointer
Example 8
Call-by-value vs Call-by-reference
???
Introduction
Some C programming tasks are performed
defined.
Can be accessed using ampersand (&)
operator.
Denotes as an address in memory.
Example 1
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
a pointer.
Step 3: Access the value at the address
available in the pointer variable.
Example 2
variable.
&variable_number refer to the address
that contains value of the variable.
*variable_pointer refer to the value inside
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.
Pointer Arithmetic
Pointer can be compared by using
Example 5 Pointer
comparison
Array of Pointer
There may be a situation when we want to
Pointer to pointer
A pointer to a pointer is a form of multiple
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)
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
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?