Professional Documents
Culture Documents
Pointers
Pointers
Pointers
POINTERS
1.Define pointers and declaration syntax of pointers with examples.
Ans: A pointer is a variable that represents the memory location of a data item, such as a
variable or an array element.Pointers contain the address of some other
variables.Additearation with a * before the variable name creates a pointer.
The declaration syntax of pointers isdatatype *pointer name
eg- int *ptr;
char *ch;
On the other hand, pointer to an array means the base address of the array.For example
void main()
{
char* test[]={"Name",Address","Ph No"};
return;
}
Here test is an array and every element of this array points to particular string.
The disadvantages of pointers area) Uninitialized pointers might cause segmentation fault.
5. Explain the term dynamic memory allocation. Also differentiate between malloc(),
calloc() and realloc() with a program as an example.
Ans: The process of allocating memory to the variables during execution of the program is
called dynamic memory allocation.
eg- int arr[100]
When above statement is executed consecutive space for 100 integers is allocated, out of
which we may be using only 10% and the rest of the space gets wasted.To overcome this
and utilise memory efficiently we dynamically allocate memory so that only the amount of
memory that is required is reserved.
Some dynamic memory allocation functions are malloc(), calloc() and realloc().
a) malloc()
The name malloc stands for "memory allocation". The function malloc() reserves a block of
memory of specified size and return a pointer of type void which can be casted into pointer
of any form.
Syntax of malloc()
ptr=(cast-type*)malloc(byte-size)
eg- ptr=(int*)malloc(100*sizeof(int));
This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes
respectively and the pointer points to the address of first byte of memory.
b) calloc()
The name calloc stands for "contiguous allocation". The only difference between malloc()
and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates
multiple blocks of memory each of same size and sets all bytes to zero.
Syntax of calloc()
ptr=(cast-type*)calloc(n,element-size);
eg.- ptr=(float*)calloc(25,sizeof(float));
This statement allocates contiguous space in memory for an array of 25 elements each of
size of float, i.e, 4 bytes.
c) realloc()
realloc() adjusts the amount of memory allocated to the block to size, copying the contents
to a new location if necessary.
Syntax of realloc()
ptr=(cast-type*)realloc(cast-type*ptr;size of (data type));
A program to differentiate between malloc(), calloc() and realloc() is#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
char *mem_allocation;
mem_allocation=malloc(20*sizeof(char));
if(mem_allocation==NULL)
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"How are you?");
}
printf("Dynamically allocated memory content: %s\n",mem_allocation);
mem_allocation=realloc(mem_allocation,100*sizeof(char));
if(mem_allocation==NULL)
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy(mem_allocation,"Space is extended upto 100 characters");
}
printf("Resized memory: %s\n",mem_allocation);
free(mem_allocation);
}
6. Differentiate between pointers to constant and constant to pointers.
Ans: pointer to constant: These type of pointers are the one which cannot change the value
they are pointing to.This means that they cannot change the
value of variable whose address they are holding.A pointer to a constant is declared as :
const int*ptr
constant to pointer: These type of pointers are the one which cannot change address they
are pointing to.This means that suppose there is a pointer which points to a variable (or
stores the address of the variable).Now if we try to point the pointer to some other variable
(or try to make the pointer store address of some other variables), then constant pointers
are incapable of this.A constant pointer is declared as:
int * const ptr
8) How can you have array of function pointers? Illustrate with examples.
Ans:
Let us consider this example:
#include<stdio.h>
int display();
int(*arr[3])();
int(*(*ptr)[3])();
int main() {
arr[0]=display;
arr[1]=getch;
ptr=&arr;
printf("%d",(**ptr)());
(*(*ptr+1))();
return 0;
}
int display(){
int num = 25;
return num;
}
Explanation:
Step 1 : Array of Function
int(*arr[3])();
above statement tell that
arr is an array of size 3.
Array stores address of functions
Array stores address of function having integer as return type and does not takes
any parameter.
{
int *ptr = (int *) malloc(sizeof(int));
return;
}
#include<stdio.h>
main()
{
int *p
p=increment(1);
printf(%d;*p);
}
int *increment(intn)
{
int *ptr;
ptr=(int*)malloc(size of (int));
*ptr=n+1;
return ptr;
}
Ans:
Output will be: 57 59 79
14.Write a program to print altenate letters of a string.
Ans:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char name[30],i,*p[30],j;
printf("\nEnter a string");
gets(name);
for(i=0,j=0;name[i]!='\0';i=i+2,j++)
{
p[j]=&name[i];
printf("%c",*p[j]);
}
return 0;
}
Output Screen:
15. Write a program to obtain the number of elements from the user, store it
dynamically in memory and find out the smallest element in the list.
Ans:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,*p,t;
printf("\nHow many numbers do you want to enter?");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
printf("\nEnter numbers");
for(i=0;i<n;i++)
{
scanf("%d",p+i);
}
for(i=1;i<n;i++)
{
if(*p>*(p+i))
{
*p=*(p+i);
}
}
printf("\nSmallest number of the numbers entered=%d",*p);
return 0;
}
Output Screen:
scanf("%d",p+i);
}
for(i=1;i<n;i++)
{
if(*p<*(p+i))
{
*p=*(p+i);
}
}
printf("\nLargest number of the numbers entered=%d",*p);
return 0;
}
Output Screen:
printf("Enter a string\n");
gets(string);
reverse(string);
return 0;
}
length = string_length(string);
begin = string;
end = string;
begin++;
end--;
}
}
return c;
}
Output Screen:
19. Write a program to implement call by reference using functions to swap two
integer numbers.
Ans:
#include<stdio.h>
int swap(int*,int*);
int main()
{
int a,b;
printf("\nEnter the first number");
scanf("%d",&a);
printf("\nEnter the second number");
scanf("%d",&b);
printf("\nNumbers berfore swapping are: %d and %d",a,b);
swap(&a,&b);
printf("\nNumbers after swapping are: %d and %d",a,b);
return 0;
}
int swap(int *p,int *q)
{
int r;
r=*p;
*p=*q;
*q=r;
return (*p,*q);
}
Output Screen:
20. Write a program to copy a string using pointers and not using strcpy functions.
Ans:
#include<stdio.h>
int main()
{
char a[50],*p[50],i,n=0;
printf("\nEnter a string");
gets(a);
for(i=0;a[i]!='\0';i++)
{
p[i]=&a[i];
}
printf("\nThe copied string is: ");
for(i=0;a[i]!='\0';i++)
{
printf("%c",*p[i]);
}
return 0;
}
Output Screen:
printf("\nEnter range");
scanf("%d",&n);
printf("\nEnter elements in 1st array:");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
printf("\nEnter elements in 2nd array:");
for(i=0;i<n;i++)
{
scanf("%d",&q[i]);
}
a=p;b=q;
printf("\nSum of the elements of the array\n");
for(i=0;i<n;i++)
{
sum=*(a+i)+*(b+i);
printf("%d\n",sum);
}
return 0;
}
Output Screen:
22. Write a program to read and print a floating point array. The space for the array
should be allocated at the run time.
Ans:
#include<stdio.h>
int main()
{
int n,p,i;
printf("\nEnter the size of the array");
scanf("%d",&n);
float arr[n];
printf("\nEnter range of array");
scanf("%d",&p);
printf("Enter the elements in the array");
for(i=0;i<p;i++)
{
scanf("%f",&arr[i]);
}
printf("\nThe elements entered in the array are:");
for(i=0;i<p;i++)
{
printf("\n %.3f",arr[i]);
}
return 0;
}
Output Screen:
Ans:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char name[30]="HELLO WORLD",*p[30];
int i,j;
for(i=0,j=0;name[i]!='\0';i++,j++)
{
p[j]=&name[i];
printf("%c",*p[j]);
}
return 0;
}
25. Write a program to find smallest of three integers values using pointer.
Ans:
#include<stdio.h>
main()
{
int a,b,c,*p,*q,*r,small;
printf("\nEnter three numbers");
scanf("%d%d%d",&a,&b,&c);
p=&a;
q=&b;
r=&c;
small=(*p<*q&&*p<*r)?*p:(*q<*r)?*q:*r;
printf("\nSmallest of the three entered numbers= %d",small);
}
Output Screen:
Submitted By:
Partha Aich
Mechanical Department
14-12-078