Thanks to everyone who has helped me, all of your answers help me so much!!!
if the declaration is this
int array[] = {1,2,3,4,5,6,7,8}, *p= array +3;
if the value of p is 000014A2
first of all why is the value of p equal to 000014A2 when it just said *p= array +3;????
what are the values of
i) *p (answer = 4)
ii) p+3 (answer 000014AE)
iii) *(p+(*p)) (answer is 8)
how do i get those answers from the info igiven? especially part 2 and 3 are confusing me i dont know how to add letters? and pointers are confusing me. please help!
More computer help with c programming please!!!?
You need to understand the difference between
p and *p;
'p' is a pointer to memory somewhere in the computer. The value of 'p' is the memory address it points to. '*p' is the value at the memory address that 'p' points to. When you create an array, you're really creating a pointer. If you type *array, you'll see it will print out 0, which is the first value in the array. So array+3 says 'take the spot in the array 'array' points to, and go 3 places ahead', which is the fourth spot in the array, which has the value 4. So '*p' is equal to 4, however 'p' is equal to the memory address, which in this case is 3*(sizeof an int) + the memory address of the array itself. Since the size of an int is 4, that means add 12 to the value of p.
The last part says *(p + (*p)), so work it from the inside out:
*p = 4:
*(p + 4), which is saying move 4 spots further down the array
since p was equal to array[3] it will now be equal to array[7], which is 8.
I know pointers are really, really confusing when you're first learning, but you just have to keep working with them until they make sense.
Reply:You should keep in mind two things about pointers
p mean address of memory location.
*p mean value
ii) p+3 (answer 000014AE)
//000014AE is address of 3rd index which value is 4 stored
*p = array +3; // it is invalid, you should use
p = array +3; // which mean 4th index element which is "4"
therefore
*p has value 4
now
*(p + (*p)) mean
*(p + 4)
Now add 4 with 3rd index = 7
which mean address of 7th index which is 8
note ({1 is stored on 0 index , and 2 is stored on 1st Index,3 is stored on 2nd Index,4 is stored on 3rd Index, 5 is stored on 4th Index ,6 is stored on 5th Index,7 is stored on 6th index ,8 is stored on 7th Index})
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment