Ch-3 Pointers
Ch-3 Pointers
Introduction:
Uses of Pointers:
Advantages of Pointers:
Disadvantages of Pointers:
Significance of Pointers:
Types of Pointers:
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int *p=null;//p is null pointer
printf(“%d”,p);
return 0;
}
O/P:
0
O/P:
8
pointervariableName=&variableName;
*pointerVariableName
Example Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=10, *ptr;
clrscr();
ptr=&a;
AddressAtPointer+(NumberToBeAdd+BytesofMemo
ryRequiredByDatatype)
Example Program:
void main()
{
int a, *intPtr;
floa b, *floatPtr;
double c, *double c;
clrscr();
getch():
}
O/P:
intPtr value: 6356748
floatPtr value: 6356740
doublePtr value: 6356760
Example Program:
void main()
{
int a, *intPtr;
floa b, *floatPtr;
double c, *double c;
clrscr();
getch():
}
O/P:
intPtr value: 6356724
floatPtr value: 6356724
doublePtr value: 6356778
void main()
{
int a, *intPtr;
floa b, *floatPtr;
double c, *double c;
clrscr();
getch():
}
O/P:
intPtr value: 6356724
floatPtr value: 6356724
4) Comparison of Pointer:
Array of Pointers:
return 0;
}
O/P:
Value of var[0]=10
Value of var[1]=100
Value of var[2]=200
return 0;
}
O/P:
Value of names[0]= Zara Ali
Value of names[1]= Hina Ali
Value of names[2]= Nuha Ali
Value of names[3]= Sara Ali
Pointers to Pointers in C:
Example Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
int *ptr1;
int **ptr2;
int ***ptr3;
ptr1=&a;
ptr2=&ptr1;
ptr3=&ptr2;
printf(“Address of normal variable ‘a’=%u\n”,
ptr1”);
printf(“Address of pointer variable
‘*ptr1’=%u\n”, ptr2”);
printf(“Address of pointer variable
‘**ptr2’=%u\n”, ptr3”);
return 0;
}
O/P:
Address of normal variable ‘a’= 6356744
Address of pointer variable ‘*ptr1’= 6356740
Address of pointer variable ‘**ptr2’= 6356736
Ch-3 Practicals:
#include<stdio.h>
#include<conio.h>
int main()
{
int num = 10;
int *ptr = #
clrscr();
return 0;
}
O/P:
The value of variable num is 10
The address of variable ptr is 0x7fff5fbff7d8
#include <stdio.h>
#include<conio.h>
int main()
int *a,b;
clrscr();
printf("Enter any value: ");
scanf("%d",&b);
return 0;
}
O/P:
Enter any value: 45
a= 45
P-3.3: Write a C program to read and print array elements using pointers
#include <stdio.h>
#include<conio.h>
int main()
{
int arr[5];
int *ptr = arr; // initialize pointer to first element of array
clrscr();
return 0;
}
O/P:
Enter the values of the array:
10
20
30
40
50
Printing array elements using pointer:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
#include <stdio.h>
#include<conio.h>
int main()
{
int a;
int *b;
int **c;
clrscr();
b = &a; // b points to a
c = &b; // c points to b
return 0;
}
O/P:
Enter a value for a: 23
The value of a is 23
P-3.5: Write a C program to make a void pointer
#include <stdio.h>
#include<conio.h>
int main()
{
void *p=NULL; //p is a void pointer
clrscr();
printf("%d", sizeof(p));
return 0;
}
O/P:
8
#include <stdio.h>
#include<conio.h>
int main()
{
char str[100];
char *ptr;
int len = 0;
clrscr();
return 0;
}
O/P:
Enter a string: Haashim Mirza
The length of the string is 13
P-3.7: Write a C program to find out the sum of array element using
pointer
#include <stdio.h>
#include<conio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int *ptr;
int sum = 0;
clrscr();
return 0;
}
O/P:
The sum of the array is 15
P-3.8: Write a C program to give solution of a quadratic equation using
pointer
-b±√b2 -4ac
//Quadratic Equation Formula: x=
2a
#include <stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
float a, b, c, r1, r2, d;
clrscr();
printf("Enter the values of a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
2
d = b * b - 4 * a * c; // d(Determinant)=-b±√𝑏 − 4𝑎𝑐⁄2
if (d > 0) //D>0 the roots are real and unequal
{
r1 = (-b + sqrt(d)) / (2 * a);
r2 = (-b - sqrt(d)) / (2 * a);
printf("The real roots are: %f and %f", r1, r2);
}
else if (d == 0) //D=0 the roots are real and equal
{
r1 = r2 = -b / (2 * a);
printf("The roots are equal: %f and %f", r1, r2);
}
else //D<0 the roots are imaginary
{
printf("The roots are imaginary");
}
return 0;
getch();
}
O/P:
Enter the values of a, b, and c: 1 1 4
The roots are imaginary
#include <stdio.h>
#include<conio.h>
int main() {
char *names[] = {"Haashim", "Ayan", "Charlie", "Zaid", "Eve"};
int i;
clrscr();
return 0;
}
O/P:
Name 1: Haashim
Name 2: Ayan
Name 3: Charlie
Name 4: Zaid
Name 5: Eve