Reputation: 49
I am solving questions on pointers and arrays in C I think I get the concept of but I want to know if I messed up here is the original question, my answers are down below and my reasoning for them
Assume that the variables declared below are stored at the following locations. Show what value is stored as a result of each of the following statements. Assume that each statement uses the values stored by the previous statements.
int *p, *q, *r;
int a = 10; b = 25;
int c[4] = {6,12,18,24};
address variables
5000 p
5004 q
5008 r
500C a
5010 b
5014 c[0]
5018 c[1]
501C c[2]
5020 c[3]
1. p = c;
2. q = &a;
3. r = p + 2;
4. c[1] = *p;
5. c[2] = *(p + 2);
6. c[3] = *p + 2;
7. *r = *q;
8. r = q;
9. p = &c[0];
10. p++;
My answers
1.5014 because p is a pointer so it points to c, which points to c[0]'s address which is 5014
2.500c because q is set to point to a's address
3.5016 because p is now 5014, 5014+2 = 5016 (I'm not sure what this would point to since 5016 is not on the address list)
4.6 , p points to address c[0] and that element is 6
5.5016 , I don't know how this would work, you would add 5014 + 2 = 5016 but that doesn't point to anywhere according to the addresses
6.8 , p points to address c[0] where 6 is stored, 6+2 = 8
7.500c , r is set to point at q which is pointing to a's address
8.10 , q points to a which is 10 (I don't understand the difference with this one and the #7, maybe the answers should be reversed)
9). 5014, p is set 5[0]'s address
10.5015, p is incremented
Can anyone tell if I messed up?
Upvotes: 1
Views: 152
Reputation: 1922
Hope this:Pointer Arithmetic; will help you.
Pointer arithmetic is different from the usual ones. when we add a value to a pointer, the value gets multiplied by the size of type of datatype (to which the pointer points to) before getting added!
So, we have:
3.. r = p + 2
becomes: r = 5014 + 2 * sizeof(int) = 501C
5.. c[2] = *(p + 2);
becomes: c[2] = *(5014 + 2 * sizeof(int))
=> *(501C)
=> 18
7.. *r = *q
now r
points to c[2]
and q
points to a
, so
*r = *q
means c[2] = a
=> 10
8.. r = q
with this, r
which was pointing c[2]
now points to a
.
10.. p++
Increments p
by one and so p
becomes 5018
from 5014
Upvotes: 3
Reputation: 665
3.5016 because p is now 5014, 5014+2 = 5016 (I'm not sure what this would point to since 5016 is not on the address list)
This is 5008 as incr on pointer increments the address. // note you are not using *p
5.5016 , I don't know how this would work, you would add 5014 + 2 = 5016 but that doesn't point to anywhere according to the addresses
This points to value at address 5008
7.500c , r is set to point at q which is pointing to a's address
value of a (10) will be stored at adrress 5008
8.10 , q points to a which is 10 (I don't understand the difference with this one and the #7, maybe the answers should be reversed)
r will point to 5004
10.5015, p is incremented
p will be incremented by 4 (consider size of int is 4). i.e p will point to c[1]
Upvotes: 0
Reputation: 327
You are right about 7th and 8th One they should be reversed. In 7th step you are dereferencing the pointer so value which is at address of q will be placed in address which r is holding.
And for 10th step increment pointer will increase based on size of data type of which it is pointer. e.g if its int it will increase by 2 bytes or 4 bytes depending on compiler. And same applies for 3rd step as well.
Other than that everything looks good.
Upvotes: 0