recategorized by
257 views
0 0 votes

What will be the output of the following $\text{C}$ code?

#include<stdio.h>
void main()
{
    int arr[5]={10,20,30,40,50};
    int *p = (int*) (&arr+1);
    printf("\%d\%d", *(arr + 1), *(p -1));
}
  1. $10$ $50$
  2. $20$ $50$
  3. $30$ $40$
  4. $20$ $40$

1 Answer

0 0 votes

int arr[5]={10,20,30,40,50}; //for suppose base address is 1000 then address of arr+4 (last element

 in the array) is 1016.Assuming int=4B.

int *p = (int*) (&arr+1);

&arr represents address of whole array,so &arr+1 =1000+size(&arr)=1020.

p=1020.

And "arr" represents first element address which is 1000.

In the printf statement *(arr + 1)=*(1000+sizeof(int))=*(1004)=second element in the array=20

And *(p-1)=(1020-size(int))=*(1016)=last element in the array=50.

The Output is 20 50

Answer:
Position:
Show:

Related questions

1 1 vote
2 2 answers
389
389 views
Shubham Sharma 2 asked Sep 9, 2025
389 views
#include<stdio.h void main() { int arr[]={1, 2, 3, 4, 5}; int *p=arr; printf("%d", *p++); printf("%d", *(p+1)); }Find the output of the above code?$1,2$$1,3$$2,3$$1,4$
0 0 votes
1 1 answer
341
341 views
Shubham Sharma 2 asked Sep 9, 2025
341 views
Consider following C program :#include<stdio.h int main() { int x[]={2,4,6,8,10}; int a, b=0, * y = x+4; for(a = 0; a<5; a++) { b=b+(*y -a) - *(y -a); } printf("%d\n", b)...
0 0 votes
2 2 answers
389
389 views
Shubham Sharma 2 asked Sep 9, 2025
389 views
Consider the following code:#include<stdio.h void f1(char *x, char *y) { char *t1; t 1 = x; x= y; y = t 1;} void f2(char x, char y) { char *t1 ; t1 = *x; *x = *y; *y = ...
0 0 votes
1 1 answer
239
239 views
Shubham Sharma 2 asked Sep 9, 2025
239 views
Which of the following is correct way to declare a functional pointer in C?int *func (int, int);int (*func) (int, int);int (func*) (int, int);int *func* (int, int);