I am trying to understand why the output of this code would be 7 11 and I am lost. What is ptr[1][2] and ptr[2][1] referring to?
#include <stdio.h>
#define SIZE 25
int main()
{
int a[SIZE];
int *ptr[5];
for (int i = 0; i < SIZE; i++){
a[i] = i;
} //has values 0-24
ptr[0]=a; //ptr[0] is assigned with address of array a.
for (int i=1; i < 5; i++){
ptr[i] = ptr[i-1]+5; //pp[1] = 5; pp[2] = 6??
}
printf("\n%d %d",ptr[1][2],ptr[2][1] ); //output is 7 11
return 0;
}
ptr[0]=aassigns&a[0], notaper-se.ptr[0] = &a[0].ptr[1] = ptr[0] + 5 = &a[5].ptr[2] = ptr[1] + 5 = &a[10]and so on. That's what the second loop is doing.int *ptr[5]as an Array of Pointers toint[5](in other words an array of 5 pointers toint). You need to contrast with withint (*ptr)[5]which is a Pointer to Array ofint[5](in other words, a single-pointer to an array ofintwith 5-elements). With that distinction, you can look at your loop limits and for each iteration ask "What is being assigned to each pointer?"printfto the code. In the first loop, addprintf("&a[%d] = %p\n", i, (void *)&a[i]);and in the second loopprintf("ptr[%d] = %p\n", i, (void *)ptr[i]);after the assignment.