Solutions to practice questions in Pointers

Pointers in C
  1. Display the address of an integer variable and one float variable.

    #include <stdio.h>
    int main(void){
    int *poti;    // integer pointer 
    float *potf;  // float pointer 
    
    int i1=3;
    poti=&i1; // Storing the address of i1  
    printf("value of integer  : %d \n",*poti);
    
    float  f1=5.6;
    potf=&f1; // Storing the address of f1 
    printf("value of Float   : %f \n",*potf);
    return 0;
    }
  2. Declare a long variable and store its address in a pointer

    #include <stdio.h>
    int main(void){
    long *pot_l;    // integer pointer 
    long i=2147483647;
    pot_l=&i; // Storing the address of i1  
    printf("value of long  : : %ld \n",*pot_l);
    return 0;
    }
    value of long  : : 2147483647
  3. Declare a string variable and store its address in a pointer . Display the string along with its address.

    #include <stdio.h>
    int main(void){
    char *potch;  // string  pointer 
    char str[10]="plus2net";
    
    potch=str;
    
    while(*potch != '\0'){
    printf("Address : %d , value :%c \n",potch,*potch);
    potch++;
    }
    // to display the string in one line 
    potch=str; // to point to first element
    while(*potch != '\0'){
    printf("%c",*potch);
    potch++;
    }
    return 0;
    }
    Output
    Address : 6487552 , value :p
    Address : 6487553 , value :l
    Address : 6487554 , value :u
    Address : 6487555 , value :s
    Address : 6487556 , value :2
    Address : 6487557 , value :n
    Address : 6487558 , value :e
    Address : 6487559 , value :t
    plus2net
  4. Find sum of two numbers using pointers

    int *pot1,*pot2,sum, num1,num2;
     num1=100;
     num2=200;
     pot1=&num1;
     pot2=&num2;
    
     sum= *pot1 + *pot2;
          printf("sum =%d ",sum);
    Output
    sum=300
  5. Take two user input numbers and swap them using pointer and display.

       int num1,num2;
       int *pot1,*pot2,*pot3; // Pointer of an integer //
       
    printf("Number 1 : ");
    scanf("%d",&num1); // read the first number
    printf("Number 2 : ");
    scanf("%d",&num2); // read the second number 
       
    pot1=&num1; // Store the address of first number 
    pot2=&num2; // Store the address of second number 
    pot3=pot2;  // Keep the address in another pointer
    pot2=pot1;  // swap
    pot1=pot3;  // swap
    
    printf("value of Number 1 :  %d \n: ", *pot1);
    printf("value of Number 2 : %d ", *pot2);
  6. Pass two numbers to a function using pointer and display highest value.

    #include <stdio.h>
    int high(int *pot1,int *pot2){
    if(*pot1>*pot2){ // check which is highest
     return *pot1;   // return value of pot1 if it is high
    }else{
     return *pot2;  // return value of pot2 
    }
    }
    int main(void){
    int i=15,j=-15,*pot1,*pot2;
    
    pot1=&i;  // store address of i in pointer pot1
    pot2=&j;  // store address of j in pointer pot2
    
    // pass the values to function high() and get the highest
    printf("Highest  = %d \n",high(pot1,pot2));
    return 0;
    }
    Highest = 15
  7. Create one array and pass the array to a function by using pointer and then display all elements.

    #include <stdio.h>
    int array_disp(int *pot){
    int k=0;
    for(k=0;k<5;k++){
       printf("Element : %d \n", *pot);
       pot++;
    }
    }
    
    int main(void){
    int j[5]={1,2,3,4,5};
    int *pot_main;
    pot_main=&j[0];
    array_disp(pot_main);
    return 0;
    }
    Element : 1
    Element : 2
    Element : 3
    Element : 4
    Element : 5
  8. In above question increase the value of elements by 2 inside the function and then display.

    We can increase the value of the pointer by 2 and display. This is the only change in script.
    printf("Element : %d \n", *pot+2);
  9. Display highest and lowest numbers in an array using pointer.

    #include <stdio.h>
    int main(void){
    int i,num1[5]={-8,40,-5 ,6 ,7};
    int *pot; // Pointer of an integer //
    pot=&num1[0];   // assign address of num to pot //
    int high=num1[0],low=num1[0];
     for(i=0;i<5;i++){
       if(high<*pot){
        high=*pot;
       }
       if(low>*pot){
        low=*pot;	
       }
     pot++;
     }
    printf("Highest and lowest values are %d , %d ",high,low);
    }
    Highest and lowest values are 40 , -8
  10. Sort an array using pointers

    #include <stdio.h>
    int main(void){
    int num[]={-34,14,-11,15,0,-20};
    int i=0,j=0;
    int x=0;
    int *pot=&num[0];
    int *pot2=&num[0];
    for(i=0;i<6;i++){
     pot=&num[i];
     for(j=i+1;j<6;j++){
      pot2=&num[j];
      // change below line to less than to get descending order 
      if(*pot>*pot2){ 
       x=*pot;
       *pot=*pot2;
       *pot2=x;
      }
     }
    }
    //// display the array elements//////
    pot=&num[0];
    for(i=0;i<6;i++){
     printf(" %d \n",*pot);
     pot++;
    }  
    }
     -34
     -20
     -11
     0
     14
     15
  11. Write a program to print a string in revers using pointer

    #include <stdio.h>
    #include <string.h>
    int main(void){
    
    char *potch;  // Char pointer 
    char str[30]="welcome to plus2net";
    int j,length;
    potch=&str[0]; 
    length=strlen(str);
    
    for(j=length-1;j>=0;j--){
    printf("%c",*(potch+j));
    }
    
    return 0;
    }
    Output
    ten2sulp ot emoclew
  12. Print the middle char of a string, if there are even numbers of chars then print the next char from the middle.

    #include <stdio.h>
    #include <string.h>
    int main(void){
    
    char *potch;  // Char pointer 
    char str[30]="plus2net";
    int j,length;
    potch=&str[0]; 
    length=strlen(str); // length of the string
    printf("%c",*(potch+length/2));
    return 0;
    }
    Output
    2
  13. Create a function to receive an pointer of an array and a number to search. This function will return 1 if the number is found inside the array otherwise return 0.

    #include <stdio.h>
    int array_search(int *pot,int num1){
    int k=0;
    for(k=0;k<5;k++){
     if(num1==*pot){
       	return 1;
       	break; // No need to further continue
    	}
     pot++;
    }
    return 0;
    }
    
    int main(void){
    int j[5]={1,2,3,4,5}; // input array
    int *pot_main;
    int num1=7; // Number to be checked for presence
    pot_main=&j[0];
    if(array_search(pot_main,num1)==1){
    printf("Yes available");
    }else{
    printf("Not available");	
    }
    return 0;
    }
Pointers in C


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer