in Programming in C
376 views
0 votes
0 votes

answer is given 20,60

but my answer is 20,10 

in solution they given that  (&arr+1= base address of arr+ 1*6) 

how it possible . Is it true or wrong

in Programming in C
376 views

1 comment

Assume 100 is the address of total arr means 1st address..so here &arr +1= 106.

Each character size 1b. So *(arr +1)= 100+1

And *(PTR -1)= 106 -1=105

So 20 and 60 will be printed.
0
0

3 Answers

1 vote
1 vote
20, 60 is the correct answer.

Below is the output of above code: (I have added extra statements to print for your clarity)

 

20 60
ptr: 6422040
arr: 6422034
ptr-1: 6422039
arr+1: 6422035
(&arr + 1): 6422040

 

In this we are assigning &arr+1 to ptr, not just arr+1, &arr+1 will return immediate next address after the end of the array. That's why ptr contains the value of the next location after the array.
You got under the impression that ptr was pointing to 20, but actually, it is pointing to memory location just after 60.

Hope this helps.
0 votes
0 votes
20,10 should be the answer
0 votes
0 votes
arr points to first element of array and &arr points to whole array so on increment arr+1 points to next memory element while &arr+1 points to memory next location which is out of array

and so ptr-1 points last element

Related questions