You are on page 1of 23

Chapter -6 Pointers

 Topics:-

• Basic of pointers
• Pointer to pointer
• Pointer and Array
• Pointer to Array
• Array of Pointers
• Functions returning a pointer
What is Pointer ?

• Pointers are the variables that contain the address of another


variable within the memory.
 Basics of pointers

• Every data item in the computer is stored in the memory in one or more
adjacent locations depending upon it’s type.
• Computers memory is sequential collection of storage cells.

Memory Cells …………………..

Address 0 1 2 65535
Memory Organization
Continue..

• Example : int x=5;


Suppose we assign address
of x to variable p, then p is
X Variable
called the pointer, as it
Value points variable x. the link
5
between x and p is shown
Address below:
1002

Representation of x=5 5
Addr of X
In memory
P X
Pointer is a variable that
Link between variables x and p
contain the address which
is a location of another
variable in the memory.
 Advantages of Pointers :
• Pointers are more efficient in handling arrays and data tables.

• Pointers return more than one value from a function.

• Pointers can pass arrays and string from one function to another.

• Pointers can create complex data structures, such as linked lists, stacks, queues.

• Pointers allow C to support dynamic memory management.

• Pointers reduce length and complexity of programs.

• Pointers increase the execution speed and thus reduce the program execution time.
 Address(&) and Dereferencing(*) operators
• To access the address variable x, the operator & is used.
• ‘&’ is called ‘address of operator’ and it is unary operator.
• We can assign the address of x to another variable p as:
• p=&x; //Assigns 1002(address of x) to p .thus p is a pointer variable.
• Accessing the value of variable through pointer(using *
operator)
• The value represented by x, can be accessed by the expression *P,
where * is called ‘value at the address’ operator and it is a unary
operator.
• *p and x represent same value
• x and *p represent 5.
• * is called ‘dereferencing or indirection operator’.
Pointer declaration
• Syntax:
data_type *ptvar;
• Examples:
int *p;
float *abc;
char * status;
Pointer Initialization

• Syntax:
datatype *ptr_var=&addr_var;
• Example:
int a=5,x,*aptr;
aptr=&a; //assign addr of a to pointer aptr
x=*aptr; //asssign value at address pointed by ‘aptr’ to x i.e x=5
*aptr=0; // assign value 0 to a
Example
Example
Example
 Pointer Arithmetic
Operation Expression Meaning
Increment Ptr++; Increment ptr to point to next location of
same type. If ptr is a pointer to integer(int
occupie 2 byte), if ptr=2003 then ptr++ gives
ptr=2005.
If ptr is a pointer to character(char occupies 1
byte), if ptr=2003 then ptr++ gives ptr=2004.

Decrement Ptr--; Decrement ptr to point to previous location of


same type. If ptr is a pointer to integer(int
occupie 2 byte), if ptr=2003 then ptr-- gives
ptr=2001.
If ptr is a pointer to character(char occupies 1
byte), if ptr=2003 then ptr-- gives ptr=2002.
Operator Expression Meaning
Adding a ptr=ptr+8 Ptr points to 8 integer locations after current
number to location.
pointer If ptr=2003 then ptr+8 gives ptr=2011.
Subtracting a Ptr=ptr-6 Ptr points to 6 itrgrr locations before current
number from locations.
a pointer If ptr=2003 then ptr-6 gives ptr=1997.
Subtracting of Ptr=ptr1-ptr2; We can subtract one pointer from another if
one pointer and only if both are pointing to one array.
from another
pointer
 Array with pointer variable
• When an array is declared, the compiler allocates a base address and storage
to contain all the elements of the array in memory locations.

• The base address is the location of the first element (index 0) of the array.

• The compiler defines the array name as a constant pointer which contains the
address of the first element of the array.
• For Example :
int x[5] = {1,2,3,4,5};
• Suppose, the base address of x is 1000, then the five elements will be
stored as follows :
Elements ----->
Value -----> 1 2 3 4 5
Address ----->
• Here the array name x is a constant pointer that contains the address of
first element, x[0].
• Therefore x = &x[0] = 1000;
• If we declare p as an integer pointer, then we can write p = x;
therefore p = &x[0];
• Now we can access every value of array x using p++ as follows :
p = &x[0] ( = 1000)
p+1 = &x[1] ( = 1002)
p+2 = &x[2] ( = 1004)
p+3 = &x[3] ( = 1006)
p+4 = &x[4] ( = 1008)
• Now we can access array elements using pointer instead of array.
 Pointer as function arguments
• When the function change( ) is called, the address of variable x is passed
into the function change( ).
• Inside the change( ), the variable p is declared as pointer therefore p is
the address of variable x.
• The statement *p = *p + 10;
• add 10 to the value stored at the address p, so the value of x is changed
from 20 to 30. Therefore the output of the program will be 30.
Example :
main( )
{
int x;
x = 20;
change(&x);
printf(“%d\n”,x);
}
change(int *p)
{
*p = *p + 10;
}
 (IMP)Swapping two number using pointer
#include<stdio.h>
 
void swap(int *num1, int *num2)
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
} Enter the first number  : 12
  Enter the Second number : 22
int main() First number  : 22
{ Second number : 12
int num1, num2;
  printf("\nEnter the first number : ");
scanf("%d", &num1);
printf("\nEnter the Second number : ");
scanf("%d", &num2);
 
swap(&num1, &num2);
 
printf("\nFirst number  : %d", num1);
printf("\nSecond number : %d", num2);
  return (0);
}
 C Program to Read integers into an array and Reversing them
using Pointers
ptr = &arr[size - 1];

#include<stdio.h> printf("\nElements of array in reverse


#include<conio.h> order are :");
#define MAX 30
for (i = size - 1; i >= 0; i--)
void main() { {
int size, i, arr[MAX]; printf("\nElement%d is %d : ", i,*ptr);
int *ptr; ptr--;
clrscr(); }

ptr = &arr[0]; getch();


}
printf("\nEnter the size of array : ");
scanf("%d", &size); Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
printf("\nEnter %d integers into array: ", size); Elements of array in reverse order are :
for (i = 0; i < size; i++) Element 4 is : 55
{ Element 4 is : 44
scanf("%d", ptr); Element 4 is : 33
ptr++; Element 4 is : 22
} Element 4 is : 11
Thank You…

You might also like