0 votes 0 votes #include <stdio.h> int main(){ int a[] = {5,3,7,2,4}; int *p = &a[3]; p -= *p; printf("%d ",*p); return 0; } output is 3. Why 2 * sizeof(int) is doene.? Programming in C programming-in-c made-easy-test-series + – Overflow04 asked Oct 2, 2022 • edited Sep 17, 2023 by Hira Thakur Overflow04 350 views answer comment Share Follow See all 3 Comments See all 3 3 Comments reply Overflow04 commented Oct 2, 2022 reply Follow Share @Abhrajyoti00 @gatecse your opinion on this question. 0 votes 0 votes Hira Thakur commented Sep 17, 2023 reply Follow Share $ p-=*p $ can be written as $p=p-(*p)$. currently, $p$ is pointing to the third element of the array. $(*p)=2$ So $p=p-2$ is pointing to the second element of the array that is $3$ 0 votes 0 votes prasantkr.singh commented Jan 10 reply Follow Share Output is 3 . int a[] = {5, 3, 7, 2, 4};: An array a is declared and initialized with five integers. int *p = &a[3];: A pointer p is declared and assigned the address of the element at index 3 in the array a. p -= *p;: The value at the memory location pointed to by p (which is a[3], i.e., 2) is subtracted from the pointer p. This means p now points to the memory location p - 2. printf("%d ", *p);: The value at the memory location pointed to by p is printed. Since p now points to p - 2, it will print the value at a[1] (assuming zero-based indexing), which is 3. 0 votes 0 votes Please log in or register to add a comment.
Best answer 2 votes 2 votes Because it is an integer array and the command p -= p*(p* = dereferenced value of pointer p = 2) which is equal to p = p – 2, means we are decreasing the address of the pointer by 2*4 byte i.e. 8 bytes. Philosophical_Virus answered Oct 2, 2022 • edited Sep 17, 2023 by Hira Thakur Philosophical_Virus comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes It Will Print 3 int a[] = {5, 3, 7, 2, 4};: An array a is declared and initialized with values. int *p = &a[3];: A pointer p is declared and initialized with the address of the fourth element of the array (a[3]), which is 2. p -= *p;: This line is equivalent to p = p - *p;. The value at the address p is dereferenced (*p), which is 2. Then, this value is subtracted from the current value of the pointer p. So, p becomes p - 2. After this operation, p points to the second element of the array (a[1]). printf("%d ", *p);: This line prints the value at the current location pointed by p, which is a[1]. So, it prints 3. prasantkr.singh answered Jan 20 prasantkr.singh comment Share Follow See all 0 reply Please log in or register to add a comment.