Pointers
Pointers
C
CONTENTS
01 Introduction 03 Pointer to Pointer
02 Pointer Arithmetic
Pointers are the heart of C programming. It is the most distinct feature of C, which
provides power and flexibility to C. Pointers separates C from other programming
languages.
Pointers are closely related to low level memory operations. Hence, let us first
understand memory in contrast to C programming.
Understanding memory
Computer memory (RAM) is a collection of contiguous block of bytes. Where individual block is
called as cell (memory cell). Each cell has a unique numeric address (also known as physical
memory address) associated with it. These addresses starts from zero and runs up to maximum
memory size (in bytes).
For example, memory location of a 64KB RAM starts from 0 and ends to 65536 (or 0x10000)
bytes.
What happens during a variable definition.
Consider the statement int num = 10;
For the above statement, the C compiler allocates memory capable to store an integer. Let say
memory is allocated at address 0x1200.
After memory allocation, the C compiler defines a label (variable name) to access the memory
location. The label is mapped to the allocated memory.
Finally, the constant 10 is stored at 0x1200. Whenever you refer num inside your program,
internally C refers to the memory location of num.
What is a pointer?
A pointer is a variable that stores memory address. If it is a variable, it must have a
valid C data type. Yes, every pointer variable has a data type associated with it. Which
means an integer pointer can hold only integer variable addresses.
Note: We never say pointer stores or holds a memory location. Instead, we say pointer
points to a memory location. So from now always use the language pointer points to a
memory location.
#include <stdio.h>
int main()
{
int num = 10;
return 0;
}
Dereference operator *
Once you have a memory address, you must be willing to get value
stored at that memory address, for that we need to dereference the
memory address.
*memory-address-or-pointer-variable;
* C program to use dereference operator
#include <stdio.h>
int main()
{
int num = 10;
/*
* &num gets the address of num
* and (*(&num)) gets value pointed by
(&num)
*/
printf("Value of num = %d\n", *(&num));
return 0;
}
How to declare pointer variable
Once you got basics of memory addresses, reference and dereference
operator. Let us declare our first pointer variable.
data-type * pointer-variable-name;
printf("Value pointed by ptr = %d \n\n", *ptr);, here * dereferences value pointed by ptr
and prints the value at memory location pointed by ptr.
Next, we made some changes to num i.e. num=10. After changes printf("Value of num
= %d \n", num); prints 10.
Since we made changes to our original variable num, hence changes are reflected back
to pointer that points to the num. *ptr in line 23, dereferences value pointed by ptr i.e.
10.
*ptr = 100; says assign 100 to memory location pointed by ptr. Which means, assign
100 to num indirectly.
Since, we again modified the value of num using *ptr = 100. Hence, num and *ptr in
line 28 and 29 will evaluate to 100.
Pointer arithmetic in C programming
Or in simple terms, incrementing a pointer will cause the pointer to point to a memory location skipping N
bytes from current pointed memory location. Where N is size of pointer data type.
Similarly, decrement operator returns the previous address pointed by the pointer. The returned address is the
difference of current pointed address and size of pointer data type.
If an integer pointer ptr pointing at 0x1230, after ptr++ it will point at 0x1234 (assuming integer size is 4
bytes).
If a character pointer cptr pointing at 0x1250, after cptr++ it will point at 0x1251 (since character occupies 1
byte).
#include <stdio.h>
#define SIZE 5
int main()
{
int arr[SIZE] = {10, 20, 30, 40, 50};
int *ptr;
int count;
count = 0;
count++;
}
return 0;
}
Pointer addition and subtraction
Pointer increment operation increments pointer by one. Causing it to
point to a memory location skipping N bytes (where N is size of pointer
data type).
count++;
}
return 0;
}
Pointer comparison
In C, you can compare two pointers using relational operator. You can
perform six different type of pointer comparison <, >, <=, >=, == and !
=.
Following are some rules that you must mind while performing pointer arithmetic.
int *ptr3 = ptr2 - ptr1; // ERROR -> (ptr2 - ptr1) evaluates to integer not integer pointer
Result of pointer and integer addition or subtraction is a pointer. For example,
int arr[] = {10, 20, 30, 40, 50};
You must not use multiplication and division operator with pointers.
Pointer to Pointer (Double Pointer) in C
We learned to create pointers to int and char. In real, you can have
pointer to any type in C. You can have a pointer to int, char, float, double,
structure, array or even pointer. In fact, you can declare pointer to
pointer to pointer to pointer. That looks complex. For now, let us focus on
pointer to pointer.
printf("Value of dPtr = %d \n", dPtr); // Print value of dPtr, i.e address of ptr
printf("Value of ptr = %d \n", *dPtr); // Print value of ptr, i.e. address of num
printf("Value of num = %d \n", **dPtr); // Print value of num
C program to demonstrate use of
pointer to a pointer /* Assigns 100 using pointer to
*/ integer */
*ptr = 100;
#include <stdio.h> printf("Value of num = %d \n", Value of num = 10
num); Value pointed by ptr
int main() printf("Value pointed by ptr = = 10
{ %d \n", *ptr); Value pointed by
int num; // Integer variable printf("Value pointed by dPtr = dPtr = 10
%d \n\n", **dPtr); Value of num = 100
int *ptr = # // Pointer to Value pointed by ptr
integer = 100
int **dPtr = &ptr; // Pointer to /* Assigns 1000 using pointer to Value pointed by
integer pointer integer pointer */ dPtr = 100
**dPtr = 1000;
/* Change the value of num printf("Value of num = %d \n", Value of num =
1000
directly */ num);
Value pointed by ptr
num = 10; printf("Value pointed by ptr = = 1000
printf("Value of num = %d \n", %d \n", *ptr); Value pointed by
num); printf("Value pointed by dPtr = dPtr = 1000
printf("Value pointed by ptr = %d \n\n", **dPtr);
%d \n", *ptr);
printf("Value pointed by dPtr = return 0;
%d \n\n", **dPtr); }
THANK
S