Pointers_Exercises
Pointers_Exercises
1. Write a program in C to demonstrate the use of & (address of) and *(value at address)
operator.
Expected Output:
m = 300
fx = 300.600006
cht = z
address of m = 0x7ffda2eeeec8
address of fx = 0x7ffda2eeeecc
address of cht = 0x7ffda2eeeec7
address of m = 0x7ffda2eeeec8
address of fx = 0x7ffda2eeeecc
address of cht = 0x7ffda2eeeec7
#include <stdio.h>
int main(){
int m=300;
float fx=300.600006;
char cht='z';
int *ptr_m=&m;
float *ptr_fx=&fx;
char *ptr_cht=&cht;
printf("Pointer: Demonstrate the use of & and * operator:\n");
printf("m= %d\n",m);
printf("fx= %f\n",fx);
printf("cht= %c\n",cht);
printf("\n");
printf("Using & operator:\n");
printf("========================================================\n");
printf("address of m= %p\n", (void*)&m);
printf("address of fx= %p\n", (void*)&fx);
printf("address of cht= %p\n", (void*)&cht);
printf("\n");
printf("Using & and * operator:\n");
printf("========================================================\n");
printf("value at address of m= %d\n", *(&m));
printf("value at address of fx= %f\n", *(&fx));
printf("value at address of cht= %c\n", *(&cht));
printf("\n");
printf("Using only pointer variable:\n");
printf("========================================================\n");
printf("address of m= %p\n", (void*)ptr_m);
printf("address of fx= %p\n", (void*)ptr_fx);
printf("address of cht= %p\n", (void*)ptr_cht);
printf("\n");
printf("Using only pointer operator:\n");
printf("========================================================\n");
printf("value at address of m= %d\n", *ptr_m);
printf("value at address of fx= %f\n", *ptr_fx);
printf("value at address of cht= %c\n", *ptr_cht);
printf("\n");
return 0;
}
#include <stdio.h>
int main(){
int num1, num2, num3, sum;
int *p1, *p2, *p3; //pointers
//user input
printf("Input first number: ");
scanf("%d", &num1);
printf("Input second number: ");
scanf("%d", &num2);
printf("Input third number: ");
scanf("%d", &num3);
p1=&num1;//assigning the addresses to the pointers
p2=&num2;
p3=&num3;
//calculation
sum=*p1+*p2+*p3;
printf("\n");
printf("===================================\n");
//result
printf("The sum of the numbers entered is: %d\n", sum);
return 0;
}
int main(){
int a, b, result;
//user input
printf("Input the first number: ");
scanf("%d",&a);
printf("Input the second number: ");
scanf("%d",&b);
add(&a, &b, &result);//calling and inputing the addresses to the other variables
printf("---------------------------------------------\n");
printf("The sum of %d and %d is %d\n", a, b, result);//result
return 0;
}
//the other function
void add(int *c, int *d, int *sum){
*sum=*c+*d;
}
4. Write a program in C to store n elements in an array and print the elements using pointer.
Test Data:
Input the number of elements to store in the array :5
Input 5 number of elements in the array:
element - 0: 5
element - 1: 7
element - 2: 2
element - 3: 9
element - 4: 8
Expected Output:
The elements you entered are:
element - 0: 5
element - 1: 7
element - 2: 2
element - 3: 9
element - 4: 8
#include <stdio.h>
int main(){
int n,i;
int arr[n];
int *ptr=arr;//pointer and assign it the base address of the arr
return 0;
}
Expected Output:
The elements in the array after sorting:
element - 1: 15
element - 2: 25
element - 3: 45
element - 4: 82
element - 5: 89
#include <stdio.h>
int main(){
int n, i;
printf("Input the number of elements to store in the array: ");//size of the arr and
num of elements
scanf("%d", &n);
int arr[n];
int *ptr=arr;//pointer to arr
printf("========================================================\n");
printf("The elements in the array after sorting:\n");//result
for(i=0;i<n;i++){
printf("element- %d: %d\n",i+1,*(ptr+i));
}
return 0;
}