0% found this document useful (0 votes)
26 views

Pointers Programs

Uploaded by

Tejashwi tejuu
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)
26 views

Pointers Programs

Uploaded by

Tejashwi tejuu
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
You are on page 1/ 10

Program to add two numbers using pointers.

#include <stdio.h>
int main()
{
int fno, sno, *ptr, *qtr, sum;

printf("\n\n Pointer : Add two numbers :\n");


printf("--------------------------------\n");

printf(" Input the first number : ");


scanf("%d", &fno);
printf(" Input the second number : ");
scanf("%d", &sno);

ptr = &fno;
qtr = &sno;

sum = *ptr + *qtr;

printf(" The sum of the entered numbers is : %d\n\n",sum);

return 0;
}

Sum of all elements in an array


#include <stdio.h>
void main()
{
int arr1[10];
int i,n, sum = 0;
int *pt;

printf("\n\n Pointer : Sum of all elements in an array :\n");


printf("------------------------------------------------\n");

printf(" Input the number of elements to store in the array (max 10) : ");
scanf("%d",&n);

printf(" Input %d number of elements in the array : \n",n);


for(i=0;i<n;i++)
{
printf(" element - %d : ",i+1);
scanf("%d",&arr1[i]);
}

pt = arr1; // pt store the base address of array arr1

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


sum = sum + *pt;
pt++;
}
printf(" The sum of array is : %d\n\n", sum);
}

Write a program in C to find the maximum number between two


numbers using a pointer.
#include <stdio.h>
#include <stdlib.h>
void main()
{
int fno,sno,*ptr1=&fno,*ptr2=&sno;

printf("\n\n Pointer : Find the maximum number between two numbers :\n");
printf("------------------------------------------------------------\n");

printf(" Input the first number : ");


scanf("%d", ptr1);
printf(" Input the second number : ");
scanf("%d", ptr2);

if(*ptr1>*ptr2)
{
printf("\n\n %d is the maximum number.\n\n",*ptr1);
}
else
{
printf("\n\n %d is the maximum number.\n\n",*ptr2);
}

Function returning a pointer


#include <stdio.h>
int* findLarger(int*, int*);
void main()
{
int numa=0;
int numb=0;
int *result;
printf("\n\n Pointer : Show a function returning pointer :\n");
printf("--------------------------------------------------\n");
printf(" Input the first number : ");
scanf("%d", &numa);
printf(" Input the second number : ");
scanf("%d", &numb);

result=findLarger(&numa, &numb);
printf(" The number %d is larger. \n\n",*result);
}

int* findLarger(int *n1, int *n2)


{
if(*n1 > *n2)
return n1;
else
return n2;
}

Count the number of vowels and consonants


#include <stdio.h>
int main()
{
char str1[50];
char *pt;
int ctrV,ctrC;
printf("\n\n Pointer : Count the number of vowels and consonants :\n");
printf("----------------------------------------------------------\n");
printf(" Input a string: ");
fgets(str1, sizeof str1, stdin);

//assign address of str1 to pt


pt=str1;

ctrV=ctrC=0;
while(*pt!='\0')
{
if(*pt=='A' ||*pt=='E' ||*pt=='I' ||*pt=='O' ||*pt=='U' ||*pt=='a' ||*pt=='e' ||*pt=='i' ||*pt=='o' ||*pt=='u')
ctrV++;
else
ctrC++;
pt++; //pointer is increasing for searching the next character
}

printf(" Number of vowels : %d\n Number of consonants : %d\n",ctrV,ctrC-1);


return 0;
}

What does the following fragment of C program print?

char c[]="GATE2011';

char *p=c;

printf("%s",p+p[3]-p[1]);

1. GATE2011
2. E2011
3. 2011
4. 011

Consider the following function implemented in C:

void printxy(int x, int y)


{
int *ptr;
x=0;
ptr=&x;
y=*ptr;
*ptr=1;
printf("%d",%d",x,y);
}
The output of invoking printxy(1,1) is

1. 0, 0
2. 0, 1
3. 1, 0
4. 1, 1
What does the following program print?

#include<stdio.h>
void f(int *p, int*q)
{
p=q;
*p=2;
}
int i=0, j=1;
int main()
{
f(&i, &j);
printf("%d%d\n",i,j);
return 0;
}
1. 2 2
2. 2 1
3. 0 1
4. 0 2
#include<stdio.h>

int main()

int a = 5;

int* p = &a;

printf("%d", ++*p);

→ p is pointing to the address of a. *p will return the content of a which is 5 and ++ will

increment it to 6.

C program to change the value of constant integer using


pointers
Since, we cannot change the value of a constant but using pointer we can change it, in this program we
are doing the same. Here, we have an integer constant and changing its value using pointer.

/*C program to change the value of constant integer using pointers.*/

#include <stdio.h>
int main()
{
const int a=10; //declare and assign constant integer
int *p; //declare integer pointer
p=&a; //assign address into pointer p

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

//assign value using pointer


*p=20;

printf("\nAfter changing - value of a: %d",a);


printf("\nWauuuu... value has changed.");

return 0;

Output

Before changing - value of a: 10


After changing - value of a: 20
Wauuuu... value has changed.

/*C program to print a string using pointer.*/


#include <stdio.h>
int main()
{
char str[100];
char *ptr;

printf("Enter a string: ");


gets(str);

//assign address of str to ptr


ptr=str;

printf("Entered string is: ");


while(*ptr!='\0')
printf("%c",*ptr++);

return 0;
}

Output

Enter a string: This is a test string.


Entered string is: This is a test string.

C program to print sizes of different type of pointers


/*C program to print size of different types of pointer variables.*/
#include <stdio.h>
int main()
{
printf("\nsize of char pointer: %d" ,sizeof(char*));
printf("\nsize of int pointer: %d" ,sizeof(int*));
printf("\nsize of float pointer: %d" ,sizeof(float*));
printf("\nsize of long int pointer: %d" ,sizeof(long int*));
printf("\nsize of double pointer: %d\n" ,sizeof(double*));
return 0;
}

Output

size of char pointer: 4


size of int pointer: 4
size of float pointer: 4
size of long int pointer: 4
size of double pointer: 4

C program for double pointer (pointer to pointer)


/*C program to demonstrate example of double pointer (pointer to pointer).*/
#include <stdio.h>

int main()
{
int a; //integer variable
int *p1; //pointer to an integer
int **p2; //pointer to an integer pointer

p1=&a; //assign address of a


p2=&p1; //assign address of p1

a=100; //assign 100 to a

//access the value of a using p1 and p2


printf("\nValue of a (using p1): %d",*p1);
printf("\nValue of a (using p2): %d",**p2);

//change the value of a using p1


*p1=200;
printf("\nValue of a: %d",*p1);
//change the value of a using p2
**p2=200;
printf("\nValue of a: %d",**p2);

return 0;
}

Output

Value of a (using p1): 100


Value of a (using p2): 100
Value of a: 200
Value of a: 200

C program for array of pointers


/*C program to demonstrate example of array of pointers.*/
#include <stdio.h>

int main()
{
/*declare same type of variables*/
int a,b,c;

/*we can create an integer pointer array to


store the address of these integer variables*/
int *ptr[3];

/*assign the address of all integer variables to ptr*/


ptr[0]= &a;
ptr[1]= &b;
ptr[2]= &c;

/*assign the values to a,b,c*/


a=100;
b=200;
c=300;

/*print values using pointer variable*/


printf("value of a: %d, b: %d, c: %d\n",*ptr[0],*ptr[1],*ptr[2]);

/*add 10 to all values using pointer*/


*ptr[0] +=10;
*ptr[1] +=10;
*ptr[2] +=10;
printf("After adding 10\nvalue of a: %d, b: %d, c:
%d\n",*ptr[0],*ptr[1],*ptr[2]);

return 0;
}

Output

value of a: 100, b: 200, c: 300


After adding 10
value of a: 110, b: 210, c: 310
NULL pointer
The word "NULL" is a constant in C language and its value is 0. In case with the pointers - if any pointer
does not contain a valid memory address or any pointer is uninitialized, known as "NULL pointer". We
can also assign 0 (or NULL) to make a pointer as "NULL pointer".

Example:

In this example, there are 3 integer pointers ptr1, ptr2 and ptr3. ptr1 is initialized with the address of
the integer variable num, thus ptr1 contains a valid memory address. ptr2 is uninitialized
and ptr3 assigned 0. Thus, ptr2 and ptr3 are the NULL pointers.

Program:

#include <stdio.h>

int main(void) {
int num = 10;

int *ptr1 = &num;


int *ptr2;
int *ptr3=0;

if(ptr1 == 0)
printf("ptr1: NULL\n");
else
printf("ptr1: NOT NULL\n");

if(ptr2 == 0)
printf("ptr2: NULL\n");
else
printf("ptr2: NOT NULL\n");

if(ptr3 == 0)
printf("ptr3: NULL\n");
else
printf("ptr3: NOT NULL\n");

return 0;
}

Output

ptr1: NOT NULL


ptr2: NULL
ptr3: NULL

You might also like