edited by
345 views
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.?

edited by

2 Answers

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.
edited by
0 votes
0 votes

         It Will Print 3

  1. int a[] = {5, 3, 7, 2, 4};: An array a is declared and initialized with values.

  2. 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.

  3. 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]).

  4. printf("%d ", *p);: This line prints the value at the current location pointed by p, which is a[1]. So, it prints 3.

Related questions

2 votes
2 votes
4 answers
1
srestha asked Apr 24, 2019
3,819 views
Which of the following data structure is efficient to implement priority queue such as insertion ,deletion, searching?A)Linked ListB)HeapC)Sorted ArrayD)Unsorted ArrayHow...
2 votes
2 votes
1 answer
3
2 votes
2 votes
0 answers
4
srestha asked Jan 28, 2019
655 views
void foo(int n) { for(i1=1;i1<=n;i1++) { for(i2=1;i2<=i1;i2++) { ....... { for(i6=1;i6<=i5;i6++) { count++; } } } } }Count initially 0.What is value returned by foo(8)?