C PROGRAMMING LANGUAGE
The following code written in C programming language was run and displays 997705320. Explain why and how this program outputs this number.
int main(void) {
int a = 5, b =20;
int* ptr = &a;
int** ptr2 = &ptr;
printf("%d\n", ((int)*ptr * (int)*ptr2));
return 0;
}
The program :
int main(void) {
int a = 5, b =20;
int* ptr = &a;
int** ptr2 = &ptr;
printf("%d\n", ((int)*ptr * (int)*ptr2));
return 0;
}
EXPLANATION :
Here , ptr is the pointer that is pointing to the variable a . And **ptr2 is the double pointer which stores the address of pointer variable ptr.
And at each time when this code runs , the memory allocated to pointer ptr and ptr2 is different.
Hence when you runs this code, memory allocated to ptr is 199541064.
And so value of (int)*ptr is 5 as ptr is pointing to variable a. And the value of (int)*ptr2 is 199541064
printf("%d\n", ((int)*ptr * (int)*ptr2));
And the above statement will print : ( 5 * 199541064 ) = 997705320
Get Answers For Free
Most questions answered within 1 hours.