0% found this document useful (0 votes)
4 views44 pages

Pointerpdf

The document provides an extensive overview of pointers in C programming, explaining their definition, usage, and various operations such as dereferencing and pointer arithmetic. It covers pointer variable declaration, initialization, and their relationship with arrays and two-dimensional arrays. Additionally, it includes code examples to illustrate concepts like accessing pointer values, pointer expressions, and the use of pointers in functions.

Uploaded by

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

Pointerpdf

The document provides an extensive overview of pointers in C programming, explaining their definition, usage, and various operations such as dereferencing and pointer arithmetic. It covers pointer variable declaration, initialization, and their relationship with arrays and two-dimensional arrays. Additionally, it includes code examples to illustrate concepts like accessing pointer values, pointer expressions, and the use of pointers in functions.

Uploaded by

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

Prepared by:

Sushant
Paudel
Pointer
Pointer is a special type of variable that holds the address of another variable. We
can have pointer of any data types. When defining a pointer, the programmer tells
exactly what kind of object the pointer is designed to “point to”. To use pointer
correctly, a pointer must be assigned a valid address of the proper type of object.
Consider the declaration,
int i = 3 ;
This declaration tells the C compiler to:
a. Reserve space in memory to hold the integer value.
b. Associate the name i with this memory location.
c. Store the value 3 at this location.
We may represent i 's location in the memory by the following memory map:

Figure 1
We can print this address through the following statement:
printf ( "\nAddress of i = %u", &i )
Here the '&' used in this statement is address of' operator. The expression &i returns
the address of the variable i, which in this case happens to be 6485.
#include<stdio.
h>
#include<conio
.h> int main()
{
int i=3;
printf("%d\n",i);
printf("%u\n",&i
); getch();
return 0;
}
Address of Operator
• & is known as address of operator
• It is an unary operator
• Operand must be the name of variable •
&operator gives the address number of
variable
• & is also known as referencing operator
Indirection Operator *
• * is indirection operator
• It is also known as dereferencing
operator • It is an unary operator
• It takes address as an argument • *
returns the content whose address is
its agrument.
Pointer variable declaration
To declare pointer variable we need to use * (asterisk) operator
(indirection/dereferencing operator or value at address) before the
variable and after data type. Pointer can only point to variable of the
same data type.
Syntax:
datatype *ptrname;
This tells the compiler three things about the ptrname.
The asterisk (*) tells that the variable ptrname is a pointer variable.
Ptrname needs a memory location i.e. the address of another variable.
Ptrname points to a variable of type data type.
For example:
int *p;// declare the variable p as a pointer variable that points to an integer
data type.
float *x;// declare the variable x as a pointer variable that points to an
floating point variable.
Initialization of pointer
Once the pointer variable has been declared, it can
be made to point to a variable using an
assignment operator such as:
Pointervariable = &normal variable;
e.g.
int i=3,
int *j;
j=&i;

Let us go by the meaning of *. It stands for 'value at


address'. Thus, int *j would mean:
1. value at address stored in j is an int 2. j
contains address of an int
3. j points to an int
4. j is a pointer which points in the direction
of an int
So we can conclude that pointer is a variable
which contains address of another variable.
Accessing value of pointer variable
Pointer variable can be accessed by using an unary operator * (known as indirection
operator or value at address). Consider the following example:
void main() y = *ptr; 4104
4108
x
int x,y; 10 4106
int *ptr; y
x = 10; 4104 10 Output:
ptr = &x;
ptr
printf(“value of x is %d”, x); printf(“Now x =%d”,x);
printf(“%d is stored at address %u”, x, }
&x); printf(“%d is stored at address Value of x is 10
%u”,*(&x), &x); printf(“%d is stored at 10 is stored at address 4104 10 is stored
address %u”, *ptr, ptr); printf(“%d is at address 4104 10 is stored at address
4104 10 is stored at address 4104 4104
stored at address %u”,y, &(*ptr));
is stored at address 4106
printf(“%d is stored at address %u”, ptr, 10 is stored at address 4108 Now x = 25
&ptr); printf(“%d is stored at address
%u”, y, &y); *ptr = 25;
Pointer expression:
Like any other variables, pointer variables can be used in expressions. Following program
illustrate the use of pointer in arithmetic expressions.
void main() y,z; a=12;
3200 p2
{ int a p1
b 4500
a,b,*p1,*p2,x, 320

b=4; 4500
p1=&a; p2=&b; 30004000

x = *p1 * *p2-6//same as x=(*p1) * (*p2)


y =4* -*p2 / *p1 +10;//same as ((4* (-(*p2)))/ (*p1)) +10;
printf("Address of a = %u",p1); getch();}
printf("Address of b = %u",p2); Address of a = 4020 Address of
printf("a=%d,b =%d",a,b); b = 4016 a = 12 , b = 4
x = 42, y = 9
printf("x=%d,y =%d",x,y); *p2
= *p2 +3;
*p1= *p2 -5;
z = *p1 * *p2 -6;
printf("a=%d,b =%d",a,b);
a = 2, b = 7
printf("z=%d",z);
z=8
#include<stdio.h>
#include<conio.h>
int main()
{
int x=5,*j;
j=&x;
printf("%d\t%u\n",x,j);
printf("%d\t%u\n",*j,&x)
; printf("%u\n",*&x);
getch();
return 0;
}
Pointer and array
When an array is declared, the compiler allocates a base
address and sufficient amount of storage to contain all the
elements of the array in contiguous memory locations. The
base address is the location of the first element (index 0) of
the array. The compiler also defines the array name as a
constant pointer to the first elements. Consider an array:
int num[] = {24, 34, 12, 44, 56, 17};
On mentioning the name of the array (i.e. num) we get its
base address (address of the zeroth element num=&num[0]).
Thus, *num refers to the first element 24. That is, *num and
*(num + 0) both refer to 24. Similarly, by saying *(num +
1), we can refer to the first element of the array i.e 34. That
means, the C compiler, internally converts num[i] to *(num
+i). So, num[i], *(num + i), and *(i + num) refers to the
same element for the same value of i.
#include<stdio.h> main()
#include<conio.h> int num[0] num[1] num[2] num[3] num[4]
2 4 5 8 1

{
65502 65504 65506 65508 65510

int a[]={2,4,5,8,1};
printf("%u\n",a);// base address
printf("%u\n",&a[0]);
printf("%d\n",a[0]);
printf("%d\n",*a);
getch();
return 0;
}
#include<stdio.h> p=&a[0];
#include<conio.h> int num[0] num[1] num[2] num[3] num[4]
main() 2

{
65502 65504 65506 65508 65510
int a[]={2,4,5,8,1}; int *p;
printf("%u\n",&a[0
]);
printf("%u\n",p);
printf("%d\n",*p
); p=p+1;
printf("%u\n",p);
printf("%d\n",*p
); getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int a[]={2,4,5,8,1};
int i;
for(i=0;i<5;i++)
{
printf("Address=%u\n",&a
[i]);
printf("Address=%u\n",(a
+i));
printf("value=%d\n",a[i]);
printf("value=%d\n",*(a+i));
printf("..............................\n
"); }
}
void main()
{
int num[] = {24, 34, 12, 44, 56, 77};
int i;
for(i=0; i<6; i++)
{
printf(“\n%d”,num[i]);
printf(“\n%d”,*(num + i)); refers to the same element
printf(“\n%d”,*(i + num));
}
}
WAP using pointers to compute the sum of all elements stored in an
array void main()
{
{
int *p,sum,i; num[0] num[1] num[2] num[3] num[4]

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

p=x;//p=&x[0]
65502 65504 65506 65508 65510
sum=0;
while(i<5)

sum = sum + *p;


p++;
i++
}
printf("Sum =%d" ,
sum); }
int fibo(int n);//Fibonacci series using recursion
int main(){
int i,num,x;
printf("enter how many terms of Fibonacci series you want to see\n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("%d\t",fibo(i));
}
getch();
return 0;
}
int fibo(int n)
{
if(n==1||n==2)
{
return 1;
}
else
{
return fibo(n-1)+fibo(n-2);
}
}
#include<stdio.h>//program to check palindrome
number; #include<conio.h>
int main()
{
int rev=0,n,n1,r;
printf("enter the number\n");
scanf("%d",&n);
n1=n;
while(n1!=0)
{
r=n1%10;
rev=rev*10+r;
n1=n1/10;
}
if(rev==n)
printf("\n%d is palindrome",n);
}
#include <stdio.h>
#include <conio.h>
void swap(int *, int *);
void main()
{
int a=50, b=100;
printf("\n Before swap function call: a=%d and b=%d", a, b);
swap(&a, &b);
printf("\n After swap function call: a=%d and b=%d", a, b);
getch();
}
void swap(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("\n Values within swap: x=%d and y=%d", *x, *y);
}
Pointer and 2-D array
Pointers can be used to manipulate two dimensional
arrays as:
A more general formula for accessing each 2-D array
element is
*(base address + row_number * number_of_columns + column_number)

Suppose, we have to access the element


num[1][0]. num[0][0] num[0][1] num[1][0] num[1][1]
12 54 55 47

65502 65504 65506 65508


Now, the expression, *(num + i * col + j) becomes
(65002 + 1 * 2 + 0). This turns out to be *(65502 +
2) equals *(65506).Value at this address is 55,
which is indeed num[1][0].
num[0][0] num[0][1] num[1][0] num[1][1]
12 54 55 47

65502 65504 65506 65508

*(65502+0*2+1)=*(65502+0+1)=*(655
04)
*(65502+1*2+0)=*(65502+2+0)=*(655
06)
*(65502+1*2+1)=*(65502+2+1)=*(655
08)
12 54
55 47

#include<stdio.h>
#include<conio.h>
void display( int *num,int, int);
void main()
{
int num[2][2] = {
{1,2},
{3,4}
};
printf("base address:%u\n",&num[0][0]);
display(&num[0][0],2,2);
getch();
}
void display(int *num,int row, int col)
{
int i,j;
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d at %u\t", *(num + i * col + j),(num + i * col + j));
}
printf("\n");
}
#include<stdio.h>
#include<conio.h>
void display( int *num,int, int);
void main()
{
int num[2][2] = {
{1,2},
{3,4}
};
display(num,2,2);
getch();
}
void display(int *num,int row, int col)
{
int i,j;
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d\t", *(num + i * col + j));
}
printf("\n");
}
}
void display(int *p, int *q, int *r, int m, int
n); void main()
{
int
m1[10][10],m2[10][10],m3[10][10],*p,*q,*r,i,j,m,n;
p=&m1[0][0];
q=&m2[0][0];
r=&m3[0][0];
clrscr();
printf("Enter row and columns");
scanf("%d%d",&m,&n);
printf("Enter element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", (p+i*n+j));
}
}
printf(" element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", *(p+i*n+j));
}
printf("\n");
}
printf("Enter element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", (q+i*n+j));
}
}
printf(" element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", *(q+i*n+j));
}
printf("\n");
}
display(p,q,r,m,n);
getch();
}
void display(int *p, int *q, int *r, int m,
int n) { int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
*(r+i*n+j)= *(q+i*n+j)+ *(p+i*n+j);
}
}
printf(" element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", *(r+i*n+j));
}
printf("\n");
}

}
#include<conio.h>//Multiplication of 2d array using
pointer #include<stdio.h>
void display(int *p, int *q, int *r, int m, int
n); void main()
{
int m1[3][3],m2[3][3],m3[3][3],*p,*q,*r,i,j,m,n;
p=&m1[0][0];
q=&m2[0][0];
r=&m3[0][0];
clrscr();
printf("Enter row and columns");
scanf("%d%d",&m,&n);
printf("Enter element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", (p+i*n+j));
}
printf(" element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", *(p+i*n+j));
}
printf("\n");
}
printf("Enter element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", (q+i*n+j));
}
}
printf(" element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", *(q+i*n+j));
}
printf("\n");
}
display(p,q,r,m,n);
getch();
}
void display(int *p, int *q, int *r, int m, int n)
{ int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
*(r+i*n+j)=0;
for(k=0;k<n;k++)
{
*(r+i*n+j)= *(r+i*n+j)+ *(p+i*n+k) * *(q+k*n+j);
}
}
}
printf(" element\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", *(r+i*n+j));
}
printf("\n");
}
}
q[0][0] q[0][1] q[0][2]
P[0][0] P[0][1] P[0][2] q[1][0] q[1][1] q[1][2]
P[1][0] P[1][1] P[1][2] q[2][0] 2[2][1] q[2][2]
P[2][0] P[2][1] P[2][2]
P[0][0] P[0][2] P[1][1] P[2][1]
P[0][1] P[1][0] P[1][2] P[2][0] P[2][2] 1 1 1 1 1 1 1 1 1
2000 2002 2004 2006 2008 2010 2012 2014 2016
q[2][1] q[2][2]
q[0][0]
q[0][1]
q[0][2] q[1][0] q[1][1] q[1][2] q[2][0]
111111111
3000
3002 3004 3006 3008 3010 3012 3014 3016 for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{
*(r+i*n+j)=0;
for(k=0;k<n;k++)
{
*(r+i*n+j)= *(r+i*n+j)+ *(p+i*n+k) * *(q+k*n+j);
}
}
}
Pointer and String
In C, string is an array of characters, terminated by a null character. Like in one
dimensional array, we can use a pointer to access the individual characters in a
string.
#WAP using pointers to determine the length of a character
string #include<stdio.h>
#include<conio.h>
int main()
{
char name[10]="NEPAL";
char *ptr;
ptr=name;
while(*ptr!='\0')
{
printf("%c",*ptr);
ptr=ptr+1;
}
getch();
return 0;
}
• Dynamic Memory Allocation
The process of allocating memory at run time is
known as dynamic memory allocation. There are
four library functions known as memory
management functions that can be used for
allocating and deallocating (freeing) memory
during program execution. They are:
• malloc()
• calloc()
• free()
• realloc()
Allocating a block of memory:
A block of memory may be allocated using the function malloc(). The malloc function
reserves a block of memory of specified size and returns a pointer of type void. This
means that we can assign it to any type of pointer. The general syntax of malloc
function is:
Syntax:
ptr=(cast-type*) malloc(byte-size);

ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of
memory with size byte-size. If the space is not sufficient to satisfy the request, then the
allocation fails and returns null pointer.
Example:
x=(int*)malloc(100*sizeof(int));

On successful execution of this statement a memory equivalent to 100 times the area of
int bytes is reserved and the address of the first byte of memory allocated is assigned to
the pointer variable x of type integer.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int *p,n,i;
clrscr();
printf("enter the value for
n(size)\n"); scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
printf("enter the
elements\n"); for(i=0;i<n;i++)
scanf("%d",(p+i));
printf("elements\n");
for(i=0;i<n;i++) {
printf("%d\n",*(p+i));
printf("%d\n",(p+i));
}
getch();
return 0;}
Allocating multiple blocks of memory:
calloc() is another memory allocation function that is normally used to
request multiple blocks of storage each of the same size and then sets
all bytes to zero. This function is normally used for storing derived data
types such as array, structures. The general form of calloc is: Syntax:
ptr=(cast-type*) calloc(n,sizeof(data type of the element)); The
above statement allocates contiguous space for n blocks each size of
elements size bytes. All bytes are initialized to zero and a pointer to
the first byte of the allocated region is returned. If there is not enough
space a null pointer is returned.
Example:
Ptr = (float*) calloc(5, sizeof(float));
This statement allocates contiguous space for 5 blocks of size four
bytes, initializes all to zero and returns the pointer of the first byte to
ptr. If there is not enough space, calloc function returns NULL
pointer.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int *p,n,i;
clrscr();
printf("enter the value for
n(size)\n"); scanf("%d",&n);
p=(int*)calloc(n,sizeof(int));
printf("enter the elements\n");

for(i=0;i<n;i++)
scanf("%d",(p+i));
printf("elements\n");
for(i=0;i<n;i++) {
printf("%d\n",*(p+i));
printf("%d\n",(p+i));
}
getch();
return 0;
}
The memory allocated by using calloc or malloc might be insufficient or excess
sometimes. In both situations we can change the memory size already allocated
with the help of the function realloc. This process is called reallocation of
memory. The general syntax of reallocation of memory is :

ptr=realloc(ptr,newsize);

This function allocates new memory space of size newsize to the pointer
variable ptr and returns a pointer to the first byte of the memory block. The
allocated new block may be or may not be at the same region.
int main()
{
int *p,n,i,n2;
clrscr();
printf("enter the value for
n(size)\n"); scanf("%d",&n);
p=(int*)calloc(n,sizeof(int));
printf("enter the
elements\n"); for(i=0;i<n;i++)
scanf("%d",(p+i));
printf("elements\n");
for(i=0;i<n;i++) {
printf("%d\n",*(p+i));
printf("%d\n",(p+i));
}
printf("enter the value for
n2(size)\n"); scanf("%d",&n2);
p=(int*)realloc(p,n2);
printf("enter new
elements\n"); for(i=0;i<n2;i++)
scanf("%d",(p+i));
printf("new
elements\n");
for(i=0;i<n2;i++)
{
printf("%d\n",*(p+i));
printf("%d\n",(p+i));
}
getch();
return 0;
}
Releasing the used space:
Compile time storage of a variable is allocated and released by
the system in automatically. But With the dynamic (runtime)
memory allocation, it is our responsibility to release the space
when it is not required. The release of storage space becomes
important when the storage is limited. There is another library
function free() that is used to free the allocated space. The
general form of free function is as:
Syntax:
free(ptr);
ptr is a pointer that has been created by using malloc or calloc.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int *p,n,i;
clrscr();
printf("enter the value for n(size)\n");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
printf("enter the elements\n");

for(i=0;i<n;i++)
scanf("%d",(p+i));
printf("elements\n");
for(i=0;i<n;i++) {
printf("%d\n",*(p+i));
printf("%d\n",(p+i));
}
free(p);
p=NULL;
getch();
return 0;
}
A pointer to a pointer is a form of multiple indirection, or a chain of pointers.
Normally, a pointer contains the address of a variable. When we define a pointer
to a pointer, the first pointer contains the address of the second pointer, which
points to the location that contains the actual value as shown below.
pointer variable
pointer

Addres Value
Addres
s
s

A variable that is a pointer to a pointer must be declared by placing


an additional asterisk in front of its name. For example, the
following declaration declares a pointer to a pointer of type int −

*p a
int **p1;**p1
3156 2156 100

3160 3156
2156
#include<stdio.h>
#include<conio.h>
int main()
{
int a=100;
int *p;
int **p1;
p=&a;
p1=&p;
clrscr();
printf("value of A
%d\n",a);
printf("%d\n",*p);
printf("%d\n",**p1)
; getch();
return 0;
}
Pointer Arithmetic
Following operations can be performed on a pointer:
Addition of a number to a pointer. For example,
int i = 4, *j, *k ;
j = &i ;
j=j+1;
j=j+9;
k=j+3;

Subtraction of a number from a pointer. For


example, int i = 4, *j, *k ;
j = &i ;
j=j-2;
j=j-5;
k=j-6;

Subtraction of a pointer from a pointer. For


example, int i = 4, j = 5, *p, *q, d ;
p = &i ;
q = &j ;
d=q-p;
4. Do not attempt the following operations on pointers. They would never work out.

(a) Addition of two pointers


(b) Multiplication of a pointer with a constant
(c) Division of a pointer with a constant
Advantages of pointer
The pointer has the following advantages:

• It allows passing variables, arrays, functions, strings and structures as


function arguments.
• It supports dynamic allocation and de-allocation of memory segments •
Variables can be swapped without physically moving them. • A pointer
improves the efficiency of certain routines.
• It allows establishing links between data elements for complex data
structures such as linked lists, stacks, queues, trees and graphs. •
Pointer reduces the length and complexity of a program.

You might also like