#include <stdio.h>int main(void) { int i = 11; int *ptr1 = &i; int **ptr2 = &ptr1; int ***ptr3 = &ptr2; printf("%p, %p, %p, %p\n", ptr1, ptr2, ptr3, &ptr3); return 0;}If the program output is (where _ is part of the address that is not shown):0x_0b4, 0x_0b8, 0x_0c0, 0x_0c8At address 0x_0b4 is stored 11 .At address 0x_0c8 is stored 200 .
Question
#include <stdio.h>int main(void) { int i = 11; int *ptr1 = &i; int **ptr2 = &ptr1; int ***ptr3 = &ptr2; printf("%p, %p, %p, %p\n", ptr1, ptr2, ptr3, &ptr3); return 0;}If the program output is (where _ is part of the address that is not shown):0x_0b4, 0x_0b8, 0x_0c0, 0x_0c8At address 0x_0b4 is stored 11 .At address 0x_0c8 is stored 200 .
Solution
The program you've written in C language is creating pointers to an integer and then pointers to those pointers. Here's a step-by-step explanation:
-
int i = 11;This line declares an integeriand assigns it the value11. -
int *ptr1 = &i;This line declares a pointerptr1to an integer and assigns it the address ofi. So,ptr1points toi. -
int **ptr2 = &ptr1;This line declares a pointerptr2to a pointer to an integer and assigns it the address ofptr1. So,ptr2points toptr1. -
int ***ptr3 = &ptr2;This line declares a pointerptr3to a pointer to a pointer to an integer and assigns it the address ofptr2. So,ptr3points toptr2. -
printf("%p, %p, %p, %p\n", ptr1, ptr2, ptr3, &ptr3);This line prints the addresses stored inptr1,ptr2,ptr3, and the address ofptr3itself.
The output of the program shows the addresses of i, ptr1, ptr2, and ptr3 respectively. The addresses will vary each time the program is run because the operating system assigns these addresses dynamically.
The statement "At address 0x_0b4 is stored 11" means that the integer i with value 11 is stored at the address 0x_0b4.
The statement "At address 0x_0c8 is stored 200" seems to be incorrect or unrelated to the given program because there is no such value 200 in the program. The address 0x_0c8 is the address of ptr3 itself, not the value it points to.
Similar Questions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.