0% found this document useful (0 votes)
3 views7 pages

Pointers_Exercises

The document contains multiple C programming exercises demonstrating the use of pointers, including operations with the address of and value at address operators, adding numbers using pointers, call by reference, storing and printing array elements using pointers, and sorting an array using pointers. Each exercise includes expected output and sample code. The programs cover fundamental concepts of pointers in C programming.

Uploaded by

Kevin Coloma
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)
3 views7 pages

Pointers_Exercises

The document contains multiple C programming exercises demonstrating the use of pointers, including operations with the address of and value at address operators, adding numbers using pointers, call by reference, storing and printing array elements using pointers, and sorting an array using pointers. Each exercise includes expected output and sample code. The programs cover fundamental concepts of pointers in C programming.

Uploaded by

Kevin Coloma
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/ 7

Name: Assessment: Pointers

1. Write a program in C to demonstrate the use of & (address of) and *(value at address)
operator.

Expected Output:

Pointer: Demonstrate the use of & and * operator:

m = 300
fx = 300.600006
cht = z

Using & operator:

address of m = 0x7ffda2eeeec8
address of fx = 0x7ffda2eeeecc
address of cht = 0x7ffda2eeeec7

Using & and * operator:

value at address of m = 300


value at address of fx = 300.600006
value at address of cht = z

Using only pointer variable:

address of m = 0x7ffda2eeeec8
address of fx = 0x7ffda2eeeecc
address of cht = 0x7ffda2eeeec7

Using only pointer operator:

value at address of m = 300


value at address of fx= 300.600006
value at address of cht= z

#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;
}

2. Write a program in C to add three numbers using pointers.


Test Data:
Input the first number: 5
Input the second number: 10
Input the third number: 15
Expected Output:
The sum of the numbers entered is: 30

#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;
}

3. Write a program in C to add numbers using call by reference.


Test Data:
Input the first number: 5
Input the second number: 6
Expected Output:
The sum of 5 and 6 is 11
#include <stdio.h>

void add(int *c, int *d, int *sum);

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;

//user input for the arr size or num of elemnts


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

int arr[n];
int *ptr=arr;//pointer and assign it the base address of the arr

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


using pointer
for(i=0;i<n;i++){
printf("element- %d: ",i);
scanf("%d", ptr+i);
}

​ printf("--------------------------------------------------------\n");
printf("The elements you entered are:\n");
for(i=0;i<n;i++){
printf("element- %d: %d\n", i,*(ptr+i));//display the entered elements using
pointer dereferencing
}

return 0;
}

5. Write a program in C to sort an array using Pointer.


Test Data:
Input the number of elements to store in the array: 5
Input 5 number of elements in the array:
element - 1: 25
element - 2: 45
element - 3: 89
element - 4: 15
element - 5: 82

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>

void sort(int *ptr, int n);

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("Input %d number of elements in the array:\n", n);//inputing value of the


elements
for(i=0;i<n;i++) {
printf("element- %d: ", i+1);
scanf("%d", ptr+i);//using pointer
}
​ //call for other function in sortin the array
sort(ptr,n);

​ 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;
}

void sort(int *ptr,int n){//sorting function using pointer in ascending order


int i,j,temp;
for(i=0;i<n-1;i++){//bubble sort using pointer arithmetic
for(j=i+1;j<n;j++){
if(*(ptr+i)>*(ptr+j)){//if element i > element j= swap
temp=*(ptr+i);
*(ptr+i)=*(ptr+j);
*(ptr+j)=temp;
}
}
}
}

You might also like