1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// more pointers
#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
|
i was thinking about this code and this thing confussed me
p++; *p = 20;
does p++; means p = numbers++ ? well number++ isnt acceptable so instead i used p = numbers + 1; *p = 20; but still i want to know how the p++; *p = 20 can be the same thing as what i replaced it for
what the code do ? it equal numbers[from 0 to 4 (5 elements) ] each with p , while it gives p a different value , p => address but *p => value
so if p is a address , in p = numbers; *p = 10; consider p address was 11 , at
p++; *p = 20; it will be 12 , the 11 was for number[0] (which its address is &number[0] //which is 11) so at address 12 it will be number[2] (which its address is &number[1] //which is 12)
its a confusing point :) i saw many and ill mention some so others get advantage and dont ask about it again :D
first when i made this out i was asking but it turn to be a guide :P
btw guys same goes here
p = numbers; *(p+4) = 50;
at *(p+4) it means value in p+4 which is equivalent to p[5] so as p = numbers then p[5] = numbers[5]
btw i know somehow this is confusing :D also this
p[5] is array of 5 elements starting from 0 to 4 and not including 5 so when u mention p[0 to 4] u r using each alone but when u mention p[5] u r using all of p[{0,1,2,3,4}]
well here is another thing i found and was to make a topic asking about it
and its this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// more pointers
#include <iostream>
using namespace std;
int main ()
{
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue; // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue
*p1 = 10; // value pointed by p1 = 10
*p2 = *p1; // value pointed by p2 = value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed by p1 = 20
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
|
over here i stucked at many stuff
first p1 = p2; which go to the other ? if p1 is 11 and p2 is 12 now both is 12 and not 11
so i was saying if both is 11 and it calls first value from 11 it will find it 20 and the second value from 12 is 20 aswell cuz both is now with address 12
but that was wronggggg !
when this start it asks for first value in first place and when it find it as address 11 it takes that 10 before they r equal then when address 11 is no longer in use it keep the value of it (10) till the end and it doesnt take the new value when its address changes , i know my english sucks with a dam way so lets the universal lang talks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
so now im the compiler , watch how ill think
int firstvalue = 5, secondvalue = 15;
me says ok ill save the first value as 5 and second as 15
int * p1, * p2;
me says ok ill declare the addresses with only integers in them
p1 = &firstvalue; // p1 = address of firstvalue
ok ill put the address right there
p2 = &secondvalue; // p2 = address of secondvalue
ok ill put the address right there
*p1 = 10; // value pointed by p1 = 10
well now my *p1(firstvalue) = 10 ill save this
*p2 = *p1; // value pointed by p2 = value pointed by p1
well now *p1=*p2=firstvalue=secondvalue = 10
p1 = p2; // p1 = p2 (value of pointer is copied)
oh wait what u doing ? p1=p2 ? ok , p1=p2=12 (if p1 is 11 and p2 is 12 both now equal 12) , oh wait , i took the value of (firstvalue) from the address of 11 and now there is no address of 11 or by another words its nulled so ill keep that value u gaved me
*p1 = 20; // value pointed by p1 = 20
ok , lemme guess p1 now is of address 12 also p2 so p1=p2 and both *values equal to second value
cout << "firstvalue is " << firstvalue << endl;
yes i kept that when i lost its address (nulled) and its 10
cout << "secondvalue is " << secondvalue << endl;
yes i called this address and found its value equal to 20
return 0;
|
was trying to make it as simple as it is but not simpler
why im telling u this info that it took me an hr to figure them out ? cuz who dont worth it wont even get benefits out of it and who do then its urs :)
btw i have asked programmers about it but they dont understand why , they just used to it :D was pretty funny that no one think about what they actually doing
good luck everyone , btw Andrew Rober is AndrewxXx ;)