Programming question: From the code: { int *a, b[10] = { 1,2,3,4,5 }; a = b + 3;
ID: 3687584 • Letter: P
Question
Programming question:
From the code:
{
int *a, b[10] = { 1,2,3,4,5 };
a = b + 3; *(a + 3) = 6;
printf("%d %d %d %d %d ", b[1], *a, a[1], b[5], b[6], *(a+4));
return 0;
}
prints 2 4 5 0 6 0
I understand that b[10] means there could be 10 different numbers in the array.
Quesiton. Please correct my assumptions:
1. b[1], I assume that is the location in the array, which in this case is 2.
2. *a I believe it comes from the a=b+3 having b=1
3. a[1] not sure
4. b[5] the fifth value in the array = 5
5. b[6] there is no 6th place so the answer is 0
6. *(a+4) there is no *(a+4)
Thanks in advanced
Explanation / Answer
{
int *a, b[10] = { 1,2,3,4,5 };
a = b + 3; *(a + 3) = 6;
printf("%d %d %d %d %d ", b[1], *a, a[1], b[5], b[6], *(a+4));
return 0;
}
a contains the memory address of b+3 or memory address of b[3]
Suppose the address of b or b[0]=2293280
address of b[0]=2293284
address of b[1]=2293288
address of b[2]=2293292
address of b[3]=2293296
Then b+3 means b[3] then the memory address of b[3] is 2293296
Therefore a=b+3 i.e a=2293296
Then a+3=2293304
*(a+3)=6 means we are storing the value 6 in the memory address 2293304
1)
b[1]=2
2)
a=2293292 is nothing but &b[3](address of b[3])
*a means the value contained in that memory address.the value contained in memory address 2293292 is 4 (nothing but b[3])
3)
a[1] the value the memory address 2293296(nothing but b[4]=5)
4)
b[5] there is no value in the array so the value is 0
5)
b[6] =6(since we stored the value 6 in the memory address 2293304, *(a+3)=6)
6)
(a+4) means the memory address 2293308
*(a+4) means the value present in memory address 2293308
Therefore *(a+4)=0(since b[7]=0)
but in the print statement we are %d only 5 times so the value of *(a+4) will not print on the console.
printf("%d %d %d %d %d ", b[1], *a, a[1], b[5], b[6], *(a+4));
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.