C | Pointer Basics | Question 2
Output of following program?
# include <stdio.h> void fun( int *ptr) { *ptr = 30; } int main() { int y = 20; fun(&y); printf ( "%d" , y); return 0; } |
chevron_right
filter_none
(A) 20
(B) 30
(C) Compiler Error
(D) Runtime Error
Answer: (B)
Explanation: The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value at the address ptr. The dereference operator * is used to access the value at an address. In the statement ‘*ptr = 30’, value at address ptr is changed to 30. The address operator & is used to get the address of a variable of any data type. In the function call statement ‘fun(&y)’, address of y is passed so that y can be modified using its address.
Recommended Posts:
- C | Pointer Basics | Question 17
- C | Pointer Basics | Question 13
- C | Pointer Basics | Question 16
- C | Pointer Basics | Question 12
- C | Pointer Basics | Question 10
- C | Pointer Basics | Question 17
- C | Pointer Basics | Question 17
- C | Pointer Basics | Question 4
- C | Pointer Basics | Question 11
- C | Pointer Basics | Question 15
- C | Pointer Basics | Question 8
- C | Pointer Basics | Question 1
- C | Pointer Basics | Question 6
- C | Pointer Basics | Question 3
- C | Pointer Basics | Question 7