Lab 9 Report
Lab 9 Report
INSTITUTE OF ENGINEERING
PULCHOWK CAMPUS
Subject: C-Programming
Experiment Number: 9
Program: BAS
Checked By:
Roll: 077BAS025
POINTERS AND DYNAMIC MEMORY ALLOCATION
BACKGROUND THEORY
Pointer:
Pointer is a variable that represents the location of a data item rather than the value. A pointer variable
of particular type can hold the address of its own type only.
Pointers are useful as they are more efficient in handling arrays and data tables.
It can be used to return multiple values from a function via function arguments.
It allows passing a function as argument to other functions.
The use of pointer arrays to character strings results in saving of data storage space in memory.
It provides an efficient way for manipulating dynamic data structures, linked lists, queues,
stacks and trees.
datatype *pointer_name;
Copy
The data type of the pointer and the variable to which the pointer variable is pointing must be the
same.
Pointer Initialization:
Pointer Initialization is the process of assigning address of a variable to a pointer variable. It contains the
address of a variable of the same data type. In C language address operator & is used to determine the address
of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with
it.
int a = 10;
Pointer expression:
Pointers are valid operands in arithmetic expressions, assignment expressions and comparison
expressions. However, not all the operators normally used in these expressions are valid with pointer
variables.
Declaring variables as :
int a,b,*p,*q;
C dynamic memory allocation refers to performing manual memory management for dynamic
memory allocation in the C programming language via a group of functions in the C standard library,
namely malloc, realloc, calloc and free.
Malloc ():
The malloc() function takes a single parameter, which is the size of the requested memory area in
bytes. It returns a pointer to the allocated memory. If the allocation fails, it returns NULL. The
prototype for the standard library function is like this:
Calloc():
The calloc() function does basically the same job as malloc(), except that it takes two parameters – the
number of array elements and the size of each element – instead of a single parameter (which is the
product of these two values). The allocated memory is also initialized to zeros. Here is the prototype:
Realloc():
The realloc() function resizes a memory allocation previously made by malloc(). It takes as parameters
a pointer to the memory area and the new size that is required. If the size is reduced, data may be lost.
If the size is increased and the function is unable to extend the existing allocation, it will automatically
allocate a new memory area and copy data across. In any case, it returns a pointer to the allocated
memory. Here is the prototype:
Free():
The free() function takes the pointer returned by malloc() and de-allocates the memory. No indication
of success or failure is returned. The function prototype is like this:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
int a,b;
printf("Address of a: %u",&a);
printf("\nAddress of b: %u",&b);
getch();
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
int *p,*q;
int a,b;
p=&a;
q=&b;
printf("Address of a=%u\n",&a);
printf("Address of b=%u\n",&b);
printf("Value of p=%u\n",p);
printf("Value of q=%u\n",q);
scanf("%d%d",&a,&b);
printf("a+b=%d\n",a+b);
printf("*p+*q=%d",*p+*q);
getch();
}
OUTPUT:
2.WAP to find larger of two numbers using function and pointer. Here pass two numbers from
main() to a function that finds the larger. Display the larger one from main() without using
return statement.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int main()
int a,b;
scanf("%d%d",&a,&b);
largest(&a,&b);
getch();
return 0;
}
void largest(int *p,int*q){
if (*p<*q)
*p=*q;
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
float marks[5];
int i;
printf("%d",marks);
for (i=0;i<5;i++)
getch();
}
OUTPUT:
4. This program asks the required size of array to the user and displays the addresses of
allocated blocks.
SOURCE CODE:
#include <stdio.h>
#include <alloc.h>
void main()
int n,i;
float *address;
scanf("%d",&n);
address=(float *)calloc(n,sizeof(float));
if (address=NULL)
exit();
}
for (i=0;i<n;i++){
free(address);
OUTPUT:
5.Solve all the problems of one dimensional array of exercise eight using the concept of dynamic
memory allocation.
1.Source code:
#include <stdio.h>
#include <conio.h>
void main()
int i, num[6]={4,5,3,2,15};
for (i=0;i<6;i++){
printf("%d",*(num+i));
getch();
OUTPUT:
2. SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
int i,num[6];
for (i=0;i<6;i++)
for (i=0;i<6;i++)
printf("%d",*(num+i));
getch();
return 0;
OUTPUT:
3. SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int main(){
int i,arr[5],sum=0;
for (i=0;i<5;i++){
scanf("%d",(arr+i));
for (i=0;i<5;i++){
sum+=*(arr+i);
getch();
return 0;
OUTPUT:
4. SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
for (int i=0;i<(n/2);i++){ //running the loop upto midpoint or near midpoint
sum+=(*(arr+i)+*(arr+n-i-1));
getch();
return 0;
OUTPUT:
5. SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int main()
int arr[5],i,high,low;
for (i=0;i<5;i++){
scanf("%d",arr+i);
high=*arr;
low=*arr;
for (i=0;i<5;i++){
if (*(arr+i)>high)
high=*(arr+i);
if (*(arr+i)<low)
low=*(arr+i);
getch();
return 0;
OUTPUT:
6. SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int temp;
for(int j=i+1;j<n;j++){
if (*(arr+j)>*(arr+i)){
temp=*(arr+i);
*(arr+i)=*(arr+j);
*(arr+j)=temp;
int main()
int *arr,n;
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
scanf("%d",arr+i);
}
func(arr,n);
printf("%d\t",*(arr+i));
getch();
return 0;
OUTPUT:
7. SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
int main()
int *arr,n;
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
scanf("%d",arr+i);
*(arr+i)=pow(*(arr+i),3);
printf("%d\t",*(arr+i));
getch();
return 0;
}
OUTPUT:
CONCLUSION:
Hence, the focused objective to understand the importance of pointers and dynamic memory allocation
was achieved successfully.