0% found this document useful (0 votes)
24 views22 pages

Understanding Pointers in Programming

Uploaded by

maxfhil36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views22 pages

Understanding Pointers in Programming

Uploaded by

maxfhil36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Pointers

1
Pointer
● Computers store data in memory slots.
● Each slot has a unique memory address.
● When we create a variable, it is stored in a random particular slot of
the memory. So, each and every variable has its own unique
memory address.
● For example:

Unique memory
1000 1004 1008 1012 1016 1020
addresses

Memory slots
in Ram
int x = 20;
1000 1004 1008 1012 1016 1020
20

x
2
Pointer
● A pointer is a special type of variable which stores the address
(memory location) of another variable.
● It does NOT store the actual value of a variable.
● The general form of a pointer variable declaration is:
data_type *pointer_name;
● In this statement the asterisk (*) is being used to indicate that
this is
a pointer variable.
● The data_type of the pointer depends on the data type of the variable
whose address it is storing. For example:
int *ip; /* pointer storing the address of an integer */
double *dp; /* pointer
float *fp; /* pointer storing
storing thethe address
address of aoffloat
a double
*/ */
char /* pointer storing the address of a character */
*ch;
3
Pointer
● The general form of a pointer variable initialization is:
pointer_name = &var_name;
● Here Ampersand (&) means “the address of”.
● So, with this statement we are assigning the address of a variable to
the pointer. For example:

int x = 20; 1000 1004 1008 1012 1016 1020

int *ptr;
ptr = &x;

1000 1004 1008 1012 1016 1020


20 1008
int *ptr = &x;
x ptr

4
Referencing & Dereferencing of
●Pointers
Referencing:
■ It means taking the address of an existing variable (using &) to set a
pointer variable.
■ Ampersand (&) is known as Reference Operator.
■ For example: int x = 20; int
*ptr;
ptr = &x;
Here in the variable ptr, the address of x is stored.
● Dereferencing:
■ Dereferencing a pointer means using the * operator to retrieve the value
from the memory address that is pointed by the pointer.
■ Asterisk (*) is known as Dereference Operator.

■ For example: int n;


int n= *ptr;
Here in the variable n, the value of x is stored.

5
Double Pointer
● It is also known as chain of pointers or pointer to pointer.
● It is the process of storing the address of a pointer to another pointer.
● The general form of a pointer variable declaration is:
data_type **pointer_name;

Pointer to Actual variable


pointer of “a” Pointer to “a” with a value

ptr2 ptr1 a

2403 1004 10
Value of “a”
3403 2403 1004

Address of Address of Address of “a”


pointer “ptr2” pointer “ptr1”
6
Double Pointer

Determine the
output using
the addresses
and value of
previous slide

7
Pointer to Actual variable
pointer of “a” Pointer to “a” with a value

ptr2 ptr1 a

2403 1004 10
Value of “a”
3403 2403 1004

Address of Address of Address of “a”


pointer “ptr2” pointer “ptr1”

8
identifier ptr3 ptr2 ptr1 A

value stored 6422040 6422030 6422010 10

memory address 6422050 6422040 6422030 6422010

a = 10 &a = 6422010
*ptr1 = 10 ptr1 = 6422010 *ptr3 = 6422030
**ptr2 = 10 &ptr1 = 6422030 **ptr3 = 6422010
***ptr3 = 10 *ptr2 = 6422010 ptr3 = 6422040
ptr2 = 6422030 &ptr3 = 6422050
&ptr2 = 6422040
9
Passing Arguments
● There are two ways in which we can pass arguments to a
function:
■ Call by value
■ Call by pointer

10
Call by Value
Output:
40
10

a x

10 10
ONLY the
2403 1004
value is being
copied

11
Call by Value
Output:
40
10

a x

10 40
ONLY the
2403 1004
value is being
copied

12
Call by Pointer
Output:
40
40

a x

10 2403

2403 1004
The Address of a has been
passed to x. So now x is
pointing to the address of a
● So any change done using *x means that the change is
being done in
the value of the address x is pointing to.
13
Call by Pointer
Output:
40
40

a x

40 2403

2403 1004
The Address of a has been
passed to x. So now x is
pointing to the address of a
● So any change done using *x means that the change is
being done in
the value of the address x is pointing to.
14
Pointers Increment and Scale
● AFactor
pointer can be incremented :
■ p1 = p1 + 1;
■ p1++;
■ p1 = p1 + 2;
■ p1 = p2 + 1;
■ And so on…
● When we increment a pointer, its value is incremented by the
Scale Factor.
● Scale factor is the length/ size of the type the pointer is pointing
to.
● The size of various data types depend on the compiler
environment. The best way to get the size is using sizeof()
function.

15
Pointers Increment and Scale
Factor 1000 1002 1004 1006 1008 1010
int a = 10; 10

1000 1002 1004 1006 1008 1010


int *ptr;
1006 10
int ptr = &a;
ptr a

1000 1002 1004 1006 1008 1010


ptr++;
1010 10

ptr a

size of Integer is 4 byte. So the Scale Factor is 4.


16
Pointers and Arrays
Array
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] elements
5 6 7 8 9 10
Values
1000 1004 1008 1012 1016 1020 Address

Base Address

● We need to put the base address in the pointer and then increase or
decrease the pointer to access array elements.

int x[5] = {1, 2, 3, 4, 5};


int *ptr;
ptr = &x[0]; [Or you can write, ptr = x]

Inserting the Base Address


17
Pointers and Arrays
● Write a Program to Print the array elements using pointer

It is printing the Memory Addresses,


NOT the elements !!

18
Pointers and Arrays
● Write a Program to Print the array elements using pointer

19
Passing Array as a Parameter in Function

20
Passing Array as a Parameter in Function

21
CALL BY VALUE VS CALL BY REFERENCE

NO. CALL BY VALUE CALL BY REFERENCE

1 A COPY OF THE VALUE IS PASSED AN ADDRESS OF VALUE IS PASSED


INTO THE FUNCTION INTO THE FUNCTION

CHANGES MADE INSIDE THE CHANGES MADE INSIDE THE


FUNCTION IS LIMITED TO THE FUNCTION VALIDATE OUTSIDE OF
FUNCTION ONLY. THE VALUES OF THE FUNCTION ALSO. THE VALUES
2 THE ACTUAL PARAMETERS DO OF THE ACTUAL PARAMETERS DO
NOT CHANGE BY CHANGING THE CHANGE BY CHANGING THE
FORMAL PARAMETERS. FORMAL PARAMETERS.

ACTUAL AND FORMAL


ARGUMENTS ARE CREATED AT ACTUAL AND FORMAL
3 THE DIFFERENT MEMORY ARGUMENTS ARE CREATED AT THE
LOCATION SAME MEMORY LOCATION

22

You might also like