You are on page 1of 35

POINTERS

It is one of the most distinct and most important features of C language.

It has added power and flexibility to the language.

Pointers are powerful and very handy tool.

The benefits are:

1.

Pointers are more efficient in handling arrays.

2.

It can be used to return multiple values from a function via function


arguments.

3.

Pointers permit references to functions and thereby facilitating passing of


functions as arguments to other functions.

4.

It allow to support dynamic memory management.

5.

It reduce length and complexity of programs.

6.

They increase the execution speed and thus reduce the program execution
time.

Of course, the real power of C lies in the proper use of pointers.

Understanding pointers
Memory organisation
Memory cell

Address
0
1
2
3
:
:
:
65,535

int n;

Memory cell

n= 30;

Address
:
:

Variable

30

250
251

Value

30

:
:

Address

251

P= 250

:
65,535

Pointer is a variable which holds the address of


another variable.
It points to another variable.

Accessing the address of a variable


int n;
P = &n;
P=250
& Address operator (Address of)

WAP to print the address of a variable


#include <stdio.h>
void main()
{
int a;
float b;
a=300;
b=20.25;
clrscr();
printf("\nThe address of the variable a is %u",&a);
printf("\nThe address of the variable b is %u",&b);
getch();
}

Declaration of pointer variable

Syntax:
data_type *pt_name;

This tells the compiler that


1.

The asterisk (*) tells that the variable pt_name is a pointer.

2.

pt_name needs a memory location.

3.

pt_name points to a variable of type data_type.

Eg:

int *p;
float *x;

Initialization of pointer variables

The process of assigning the address of a variable to a pointer variable is


known as initialization.

All the pointer variables should be initialized before they are used in the
program.

int n;
int *p;

declaration

n=30;
p= &n;

initialization

int * p= &n;

float a, b;
int x, *p;
p= &a;
b= *p;

WRONG

int x, *p = x;
int *p = &x, x;
int *p=NULL;
int *p=0;
int *p = 5360;

Accessing a variable through its pointer

Once a pointer has been assigned the address of a variable, the


value of a variable can be accessed by using a variable.

It is done by * (asterisk) operator.

* is known as indirection operator or dereferencing operator.

int qty, *p,n;


qty=256;
p=&qty;
n=*p;
*p = 5;

nn==256
5

Pointer flexibility
int x,y,z,*p;
............
p=&x;
............
p=&y;

............
p=&z;
............

Pointer flexibility
int x;
int *p=&x;
int *q=&x;
int *r=&x;

POINTERS AND ARRAYS


When an array is declared, compiler allocates sufficient amount of memory to contain all the
elements of the array.
Base address which gives location of the first element is also allocated by the compiler.
Suppose we declare an array arr,
int arr[5]= {7,4,8,9,6};
Assuming that the base address of arr is 1000 and each integer requires two byte, the five
element will be stored as follows

POINTERS AND ARRAYS


Here variable arr will give the base address, which is a constant pointer
pointing to the element, arr[0].
Therefore arr is containing the address of arr[0] i.e 1000.
arr is equal to &arr[0]
int *p;
p = arr;
or p = &arr[0];

1000

Successive elements can be retrieved by incrementing a pointer (p++).

To access memory elements using a pointer


#include <stdio.h>
void main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a;
// same as int*p = &a[0]
for (i=0; i<5; i++)
{
printf(%d\n ", *p);
p++;
}

Elements
Values
Address

X[0]

X[1]

X[2]

X[3]

X[4]

1000

1002

The relationship between x and p is:

p=&x[0]

(= 1000)

p+1= &x[1]

(= 1002)

p+2= &x[2]

(= 1004)

p+3= &x[3]

(= 1006)

p+4= &x[4]

(= 1008)

1004

1006

1008

int x[5];
int *p;
p=&x;

WAP to accept n array elements and display using pointer


#include <stdio.h>

printf("\nThe array elements are\n");

void main()

for(i=0;i<n;i++)

{
int a[30];

printf("%d\n",*ptr);

int i,n,*ptr;

ptr++;

ptr=a;

printf("Enter the value of n : ");


scanf("%d",&n);
printf("\nEnter the array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

getch();
}

Pointers and strings


char str[5]=HELL0;
Or
char *str;
str=Hello
or
char *str=HELLO;

Program to accept and display a string using pointer


#include <stdio.h>
void main()
{
char *s;
clrscr();
printf("Enter the string : ");
gets(s);
printf("The enterd sting is %s",s);
getch();
}

Wap to find the length using pointer ( without strlen() )


#include <stdio.h>
void main()
{
char *s;
int len=0;
clrscr();
printf("Enter the string : ");
gets(s);
while(*s)
{

s++;
len++

}
printf("The length is %d",len);
getch();
}

Wap to find the length using pointer ( without strlen() )


#include <stdio.h>
void main()
{
char *s;
int len;
char *ptr;
ptr=s;
printf("Enter the string : ");
gets(s);
while(*ptr)
{

ptr++;
}

len=ptr-s;
printf("The length is %d",len);
getch();
}

PASSING ARGUMENTS TO FUNCTIONS

The mechanism used to convey information to the function is the argument.

Arguments (Parameters) are passed to function when calling a function.

The parameters passed to the function are called as actual parameters.


(Parameters used in function call)

Parameters given in function definition are called formal parameters.

Function Arguments:

While calling a function, there are two ways that arguments can be passed to a
function:
Pass

by value

Pass

by reference

CALL BY VALUE (PASS BY VALUE)

Thecall by valuemethod of passing arguments to a function copies the


actual value of an argument into the formal parameter of the function.

In this case, changes made to the parameter inside the function have no
effect on the argument.

Call by value is restricted to one-way transfer of information.

It allows transfer of only zero or one value only through the return
mechanism.

#include <stdio.h>
void change(int);
void main()
{
int n;
clrscr();
printf("Enter the values of n : ");
scanf("%d",&n);
printf("\nThe value of n before function call is %d",n);
change(n);
printf("\nThe value of n after function call is %d",n);
getch();
}
void change(int n)
{
n=n+30;
printf("\nThe value of n within function is %d",n);
}

LAB: WAP to show pass by value


#include <stdio.h>
void swap(int x, int y);
int main ()

void swap(int a, int b)


{
int temp;

{
// local variable declaration:
int a,b;

temp = a;

clrscr();

a= b;

printf("Enter two values of a and b : ");

b = temp;

scanf("%d %d",&a,&b);
printf("Before swap, value of a : %d", a);
printf("\nBefore swap, value of b : %d",b);
swap(a, b);
printf("\nAfter swap, value of a :%d",a);
printf("\nAfter swap, value of b : %d", b);
getch();
return 0;
}

PASS BY REFERENCE (CALL BY REFERENCE)

Thecall by pointermethod of passing arguments to a function copies the


address of an argument into the formal parameter.

Inside the function, the address is used to access the actual argument used in
the call.

This means that changes made to the parameter affect the passed argument.

When pointers are used as arguments asterisk (*) must precede the formal
pointer argument.

#include <stdio.h>
void change(int *n);

void change(int *n)

void main()

*n=*n+30;
int n;
clrscr();
printf("Enter the values of n : ");
scanf("%d",&n);
printf("\nThe value of n before function call is %d",n);
change(&n);
printf("\nThe value of n after function call is %d",n);
getch();

LAB: WAP to implement pass by reference


#include <stdio.h>
void swap(int *a, int *b);
int main ()

void swap(int *a, int *b)


{
int temp;

{
// local variable declaration:
int a,b;

temp = *a;

clrscr();
printf("Enter two values of a and b : ");

*a= *b;

scanf("%d %d",&a,&b);

*b = temp;

printf("Before swap, value of a : %d", a);


printf("\nBefore swap, value of b : %d",b);
swap(&a,&b);
printf("\nAfter swap, value of a :%d",a);
printf("\nAfter swap, value of b : %d", b);
getch();
return 0;
}

LAB: WAP to concatenate strings using pointers

#include <stdio.h>
void concatenate_string(char*, char*);
int main()
{
//char original[100], add[100];
char *str1,*str2; clrscr();
printf("Enter source string\n");
gets(str1);

void concatenate_string(char *str1, char *str2)


{
while(*str1)
str1++;
while(*str2)
{
*str1 = *str2;
str2++;
str1++;
}
*str1 = '\0';

printf("Enter string to concatenate\n");


gets(str2);
}
concatenate_string(str1,str2);
printf("String after concatenation is %s", str1);
getch();
return 0;
}

Pointer Arithmetic

Pointer can be:


i)

Incremented

ii)

Decremented

iii)

a value can be added

iv)

a value can be decremented

int a=50;

50

2004
2006

2004

5000

int *p;
p=&a;
p++;
p--;
p-2;
p+5;

2004

Array of pointers

char name[5][30];

char *name[5];