C arrays and pointers etc. Help!
I have the code
Code:
int k = 2;
int a[10];
int b = 0;
for(b=0;b<10;b++)
{
a[b] = 2;
}
printf("Normally with a[k] we get %d\n", a[k]);
int c;
c = *(int *) ((int)&a[0] + k*sizeof(int));
printf("So c = %d\n", c);
Where I'm just printing out a value from the array "a". However, I don't get why
Code:
c = *(int *) ((int)&a[0] + k*sizeof(int));
works exactly the same as a[k].
I've been told
*(int *) = indirect
(int)&a[0] = base address
k*sizeof(int) = offset
I know that the end bit selects which value in the array I want (how many objects away from the start of the array the thing I want is), but I don't get what the other two parts do.
Thanks