Prerequisite: Pointers in C/C++
- Question 1
What will be the output?#include<stdio.h>intmain(){inta[] = { 1, 2, 3, 4, 5} ;int*ptr;ptr = a;printf(" %d ", *( ptr + 1) );return0;}chevron_rightfilter_noneoutput
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. - Question 2
What will be the output?#include<stdio.h>intmain(){inta = 5;int*ptr ;ptr = &a;*ptr = *ptr * 3;printf("%d", a);return0;}chevron_rightfilter_noneOutput:
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. - Question 2
What will be the output?#include<stdio.h>intmain(){inti = 6, *j, k;j = &i;printf("%d\n", i * *j * i + *j);return0;}chevron_rightfilter_noneOutput:
222Description:
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. - Question 4
What will be the output?#include<stdio.h>intmain(){intx = 20, *y, *z;// Assume address of x is 500 and// integer is 4 byte sizey = &x;z = y;*y++;*z++;x++;printf("x = %d, y = %d, z = %d \n", x, y, z);return0;}chevron_rightfilter_noneOutput:
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. - Question 5
What will be the output?#include<stdio.h>intmain(){intx = 10;int*y, **z;y = &x;z = &y;printf("x = %d, y = %d, z = %d\n", x, *y, **z);return0;}chevron_rightfilter_noneOutput:
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.
Recommended Posts:
- Output of C++ programs | Set 38 (Pointers)
- Output of C++ programs | Set 39 (Pointers)
- Output of C++ programs | Set 47 (Pointers)
- Output of C programs | Set 64 (Pointers)
- Output of C Programs | Set 1
- Output of C Programs | Set 2
- Output of C Programs | Set 3
- Output of C Programs | Set 4
- Output of C Programs | Set 5
- Output of C Programs | Set 6
- Output of C Programs | Set 7
- Output of C Programs | Set 9
- Output of C Programs | Set 11
- Output of C Programs | Set 12
- Output of C Programs | Set 14
- Output of C Programs | Set 13
- Output of C Programs | Set 16
- Output of C++ programs | Set 50
- Output of C programs | Set 29
- Output of Java Programs | Set 12
Improved By : mohit rao 1

