You are on page 1of 20

Chapter 6- Pointers

❖ BASICS OF POINTERS
❖ P O I N T E R A N D A R R AY
❖ POINTER AND STRING
❖ A R R AY O F P O I N T E R S
❖ POINTER AS A FUNCTION ARGUMENT

Subject name: Programming and Problem Solving


Subject code: BTCO12107
Basics of Pointers
Pointer is a variable that stores
address of another variable.

VAR_NAME
Syntax:
VALUE/
Datatype *ptr_var_name; DATA
Example: ADDRESS OF
VARIABLE
int *pnum;
char *pch;
float *pfnum;

PROF. BHARGAVI S RANI CHAPTER 6 2


Advantages
1.Pointers are more efficient in handling Arrays and Structures.
2.Pointers allow references to function and thereby helps in passing of
function as arguments to other functions.
3.It reduces length of the program and its execution time as well.
4.It allows C language to support Dynamic Memory management.

PROF. BHARGAVI S RANI CHAPTER 6 3


Declaration & initialization of pointer
variable
Example:
int a = 10;
int *ptr; //pointer declaration
ptr = &a; //pointer initialization

Here: the * (asterisk symbol) specifies that ptr is a pointer variable


a variable’s address is assigned to ptr.
The & (address operator) retrieves the lvalue(address) of a
NOTE: the data type of the pointer variable and the variable to which
it points must be the same

PROF. BHARGAVI S RANI CHAPTER 6 4


Accessing value(s) using pointer

Var_name *p num
Value /
Data -12 10
Address of
variable
-14 -12
PROF. BHARGAVI S RANI CHAPTER 6 5
Swap 2 numbers using pointers

PROF. BHARGAVI S RANI CHAPTER 6 6


Pointer arithmetic
1. Operations such as increment, decrement, subtraction and addition (only on
values at the address) can be applied on pointer variables.
Example:
Addition : o = *p1+*p2;
printf("*p1+*p2 = %d\n", o);
Subtraction: p3 = p1-p2;
printf("p1 - p2 = %d\n", p3); // It subtracts the addresses of the two
variables and then divides it by the size of the pointer datatype (here integer)which gives
us the number of elements of integer data type that can be stored within it.
Increment: p1++;
printf("p1++ = %d\n", p1);
Decrement: p2--;
printf("p2-- = %d\n", p2);

PROF. BHARGAVI S RANI CHAPTER 6 7


Pointer arithmetic
2. Addition or subtraction of an integer value to a pointer is accepted
ptr2 = ptr2 + 3;
3. Pointer variables can be compared using relational operators.
if(ptr2 > ptr1)
printf("PtrB is greater than ptrA");
4. Multiplication of an integer constant to a pointer is not accepted.
ptr = ptr * 3
5. Addition, multiplication of 2 pointer variables is not accepted
ptr1 + ptr2 or ptr1 * ptr2

PROF. BHARGAVI S RANI CHAPTER 6 8


Types of Pointers
1. Wild pointer: Wild pointers are also called uninitialized pointers
int *p; //wild pointer

2. Void pointer: It is a pointer that has no associated data type with it. A void pointer can
hold addresses of any type and can be typecast to any type.
It is also called a generic pointer and does not have any standard data type.
void *p = NULL; //void pointer
3. Null pointer: create a null pointer by assigning the null value at the time of pointer
declaration.
[ Null is a built-in constant that has a value of zero ]
int *ptr = NULL; //null pointer

4. Dangling pointer: it is a pointer which points to a non-existing memory location.

PROF. BHARGAVI S RANI CHAPTER 6 9


Pointer to Pointer
A pointer holding the address of another pointer, which points to the location that has
the actual value as shown below.

The following
declaration:
declares a pointer to a
pointer of type int −

int **var;

PROF. BHARGAVI S RANI CHAPTER 6 10


Pointer and Array

X[4]

1 2 3 4 5

Output
* ptr = 3
*(ptr+1) = 4
*(ptr-1) = 2

PROF. BHARGAVI S RANI CHAPTER 6 11


Pointer to Array
▪ A pointer to array is also known as array pointer.

▪Two types of pointers to array


◦Pointer points to first element of an array
◦Pointer points to whole array

PROF. BHARGAVI S RANI CHAPTER 6 12


o Pointer points to first element of an array
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
printf(“ %p \n", ptr);
printf(" %d \n", ptr);
}

OUTPUT

PROF. BHARGAVI S RANI CHAPTER 6 13


o Pointer points to whole array
{
int arr[5] = { 1, 2, 3, 4, 5 };
int (*ptr)[5] = &arr, i ;
printf("\n\n Address of Array(in Hexadecimal ) :\n");
for(i=0;i<=4;i++)
printf(" %p ",ptr[i]);
printf("\n\n Address of Array(in integer ) :\n");
for(i=0;i<=4;i++)
printf(" %d ",ptr[i]); OUTPUT:
printf("\n\n Values in Array :\n");
for(i=0;i<=4;i++)
printf(" %d ",(*ptr)[i]);
}

PROF. BHARGAVI S RANI CHAPTER 6 14


Array of Pointers
• Just like we can declare an array of int, float or char etc, we can
also declare an array of pointers, here is the syntax to do the
same.
Syntax: datatype *array_name[size];
Example: int *arrop[5];
• Here arrop is an array of 5 integer pointers.
• It means that this array can hold the address of 5 integer
variables.

PROF. BHARGAVI S RANI CHAPTER 6 15


“Array of pointers” is an array of the pointer variables. It is also known as pointer arrays.

PROF. BHARGAVI S RANI CHAPTER 6 16


Example
int *arrop[3];
int a = 10, b = 20, c = 50, i;

arrop[0] = &a;
Expected Output:
arrop[1] = &b;
arrop[2] = &c; Address = 387130656 Value = 10
Address = 387130660 Value = 20
Address = 387130664 Value = 50
for(i = 0; i < 3; i++)
{
printf("Address = %d\t Value = %d\n", arrop[i], *arrop[i]);
}

PROF. BHARGAVI S RANI CHAPTER 6 17


Pointer and String
In the following code we are assigning the address of the string
str to the pointer ptr.

char *ptr = str;

PROF. BHARGAVI S RANI CHAPTER 6 18


Example:
char str[6] = "Hello"; // string variable
char *ptr = str; // pointer variable
while(*ptr != '\0’) // print the string
{
printf("%c", *ptr);
// move the ptr pointer to the next memory location
ptr++;
}

PROF. BHARGAVI S RANI CHAPTER 6 19


Example: sorting using pointers

PROF. BHARGAVI S RANI CHAPTER 6 20

You might also like