• edited by
814 views
0 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.?

2 Answers

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

Position:
Show:

Related questions

2 2 votes
4 4 answers
6.7k
6.7k views
srestha asked Apr 24, 2019
6,653 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...
0 0 votes
0 0 answers
1.0k
1.0k views
Ram Swaroop asked Jan 29, 2019
1,005 views
Consider the following C code:#include <stdio.h>struct MadeEasy{char p,q,r;};int main (void){struct MadeEasy a={ d' - 2019,'e',5+'a'};struct MadeEasy *b=&a;printf("%c, %c...
2 2 votes
1 1 answer
2.2k
2.2k views
Ram Swaroop asked Jan 29, 2019
2,185 views
Consider the following C code:include <stdio.h>int fun(){static int num=25;return num ;}int main(){for(fun( ); fun();fun())printf("%d", fun( ));return O;}The sum of the v...
2 2 votes
0 0 answers
1.4k
1.4k views
srestha asked Jan 28, 2019
1,361 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)?