Open In App

Output of C programs | Set 31 (Pointers)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Pointers in C/C++ 

  1. Question 1 What will be the output? 

C




#include<stdio.h>
  
int main()
    int a[] = { 1, 2, 3, 4, 5} ;
    int *ptr;
    ptr = a;
    printf(" %d ", *( ptr + 1) );
  
    return 0;
}


  1. output
  2
  1. Description: It is possible to assign an array to a pointer. so, when ptr = a; is executed the address of element a[0] is assigned to ptr and *ptr gives the value of element a[0]. When *(ptr + n) is executed the value at the nth location in the array is accessed.
  2. Question 2 What will be the output? 

C




#include<stdio.h>
  
int main()
    int a = 5;
    int *ptr ;
    ptr = &a;
    *ptr = *ptr * 3;
    printf("%d", a);
  
    return 0;
}


  1. Output:
 15
  1. Description: ptr = &a; copies the address of a in ptr making *ptr = a and the statement *ptr = *ptr * 3; can be written as a = a * 3; making a as 15.
  2. Question 2 What will be the output? 

C




     
#include<stdio.h>
  
int main()
{
    int i = 6, *j, k;
    j = &i;
    printf("%d\n", i * *j * i + *j);
    return 0;
}


  1. Output:
222
    
  1. Description: According to BODMAS rule multiplication is given higher priority. In the expression i * *j * i + *j;, i * *j *i will be evaluated first and gives result 216 and then adding *j i.e., i = 6 the output becomes 222.
  2. Question 4 What will be the output? 

C




#include<stdio.h>
  
int main()
{
    int x = 20, *y, *z;
      
    // Assume address of x is 500 and 
    // integer is 4 byte size 
    y = &x; 
    z = y;
    *y++; 
    *z++;
    x++;
    printf("x = %d, y = %d, z = %d \n", x, y, z);
    return 0;
}


  1. Output:
x=21 y=504 z=504
  1. Description: In the beginning, the address of x is assigned to y and then y to z, it makes y and z similar. when the pointer variables are incremented their value is added with the size of the variable, in this case, y and z are incremented by 4.
  2. Question 5 What will be the output? 

C




#include<stdio.h>
  
int main()
{
    int x = 10;
    int *y, **z;
    y = &x; 
    z = &y;
    printf("x = %d, y = %d, z = %d\n", x, *y, **z);
    return 0;
}


  1. Output:
x=10 y=10 z=10
  1. Description: *y is a pointer variable whereas **z is a pointer to a pointer variable. *y gives the value at the address it holds and **z searches twice i.e., it first takes the value at the address it holds and then gives the value at that address.

This article is contributed by I.HARISH KUMARs and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.



Last Updated : 14 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads