0

How the address of a variable is affected by arithmetic oprators

c = 500; //let address of c be 2004 ptr = &c; printf("%d", ptr + 1); printf("%d ", *(ptr + 2)); printf("%d", ptr - 1); // so what will be outputs and why??

4th Apr 2020, 7:19 AM
Somya Sarathi Samal
Somya Sarathi Samal - avatar
2 odpowiedzi
+ 1
Depends on the type of c. Assuming it is an int and is 4 bytes, the first output will be 2008. The second will be undefined behaviour because you are reading at an invalid memory address. The third will print 2000. This is because adding 1 to pre will increase its value by sizeof int which is 4
5th Apr 2020, 10:38 AM
The Man
The Man - avatar
0
@The Man thanks for answering. So, according to you the first one will be 2008??
5th Apr 2020, 10:51 AM
Somya Sarathi Samal
Somya Sarathi Samal - avatar