Pointers Programs
Pointers Programs
#include <stdio.h>
int main()
{
int fno, sno, *ptr, *qtr, sum;
ptr = &fno;
qtr = &sno;
return 0;
}
printf(" Input the number of elements to store in the array (max 10) : ");
scanf("%d",&n);
printf("\n\n Pointer : Find the maximum number between two numbers :\n");
printf("------------------------------------------------------------\n");
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);
}
result=findLarger(&numa, &numb);
printf(" The number %d is larger. \n\n",*result);
}
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
}
char c[]="GATE2011';
char *p=c;
printf("%s",p+p[3]-p[1]);
1. GATE2011
2. E2011
3. 2011
4. 011
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.
#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
return 0;
Output
return 0;
}
Output
Output
int main()
{
int a; //integer variable
int *p1; //pointer to an integer
int **p2; //pointer to an integer pointer
return 0;
}
Output
int main()
{
/*declare same type of variables*/
int a,b,c;
return 0;
}
Output
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;
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