Output of C programs | Set 31 (Pointers)

Prerequisite: Pointers in C/C++

  1. Question 1
    What will be the output?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    output

      2
    

    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?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:



     15
    

    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.

  3. Question 2
    What will be the output?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    222
        

    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.

  4. Question 4
    What will be the output?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    x=21 y=504 z=504

    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 there value is added with the size of the variable, in this case, y and z are incremented by 4.

  5. Question 5
    What will be the output?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    x=10 y=10 z=10

    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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : mohit rao 1



Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.