Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

Running the following code will print out orld. What is happening here? What exactly does &(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;
}
like image 853
marktani Avatar asked Dec 25 '22 12:12

marktani


1 Answers

char p[] = "HelloWorld";
int i[] = {2,1,3,5,6}, j = 4;

&(p[*(i + j)]) is evaluated as below:

here i is the base address of array i. Thus i+4 will be the address of the fifth element in the array i. *(i+j) will be equal to 6. P[6] will be o after W. &(p[*(i + j)]) would be equal to &p[6]. Thus in printf you are passing the address of o and the output would be orld.

like image 100
Rahul Avatar answered Jan 11 '23 08:01

Rahul