c - What exactly does &(p[*(i + j)]) do? -


running following code print out orld. happening here? &(p[*(i + j)]) do?

#include <stdio.h> char p[] = "helloworld"; int i[] = {2,1,3,5,6}, j = 4;  int main() {     printf(&(p[*(i + j)]));     return 0; } 

i'll try simplify step step

#include <stdio.h> char p[] = "helloworld"; int i[] = {2,1,3,5,6}, j = 4;  int main() {     printf(&(p[*(i + j)]));     return 0; } 

the first 3 lines obvious:

  • p array of 10 characters
  • i array of 5 integers
  • j integer , has value 4

printf(&(p[*(i + j)]));

is same as

printf(&(p[*(i + 4)]));

is same as

printf(&(p[*([adress of first element of i] + 4)]));

is same as

printf(&(p[*([adress of fourth element of i])]));

now have know *address gives value in address. so:

printf(&(p[6]));

now that's point guess struggling. have know:

  • arrays nothing else part in memory continuous. specified it's starting address.
  • &something gives address of something

so "slices" array helloworld orld. in python write p[6:], in c write &p[6].


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -