Tuesday, July 14, 2009

C++ and pointers? I wrote a few lines to help me understand this..please explain...easy question!?

#include %26lt;iostream%26gt;





int main(){








int* x;





int y = 4;





x = %26amp;y;





//is it correct to say something like x is a memory address and by putting %26amp;y, the compiler reads it as "take x, which is a variable that stores strictly a memory address and store the memory address which contains the content of the variable y"??





std::cout %26lt;%26lt; *x %26lt;%26lt; '\n';





int *z = x;





++*z;





//also for the above, int *z = x; is setting a variable z which strictly stores the address, to the address of x (which may or may contain data) and the ++*z is read by a compiler as (increment the value of the memory address z)





std::cout %26lt;%26lt; *z %26lt;%26lt; '\n';





return 0;


};





Once you declare a pointer such as int *ptr the rest of the block referring to ptr refers to the mem address and *ptr refers to the data at that address right?...where does %26amp;ptr fit into the picture?








Is what I am saying correct??? In your own words what does





int *ptr





ptr


%26amp;ptr


*ptr





all referring to?





Thanks!!

C++ and pointers? I wrote a few lines to help me understand this..please explain...easy question!?
See if this makes some sense to you.





If I say;





int y = 5;





I created the variable y (at this point you have no idea where in memory this variable is) and it put the 5 in that location. So refering to y in a program is now like saying get what is in y (the 5) and do something with it.


std::cout %26lt;%26lt; y %26lt;%26lt; \n;





Now say you happen to know that the memory location used for y is 1000.





If I said:





int *x = 1000; // Note the compiler is probably going to complain about this because 1000 isn't a proper integer pointer, but this is just to get the idea.


Is creating the variable x. And putting the memory location 1000 in it. Now I want to not what is in x, but what is at the location 1000, so I "dereference the pointer x". Which is given by *x, or in other words give me the value of the integer at that location that is stored in x. So in this case since x is pointing the the same location as y, *x will give me 5.





std::cout %26lt;%26lt; *x %26lt;%26lt; '\n";





Now for %26amp;y. This will give me that address (memory location) of where y is storing its value. So without knowing where y is storing its value I can do it like this:





int *x = %26amp;y;





That creates x and sets it to the memory location where y is storing its interger value. And is the proper way to set the pointer x because I'm setting the integer pointer x to the memory location where y is storing its value.





Note that even though the syntax says;


int *x;


*x





These are actually not the same. The *x is just the way to declare a pointer to an integer.





Where as *x is tell it to given you the integer value from where x is pointing to.
Reply:int *ptr /*this delcairs a pointer of type int. Which means that the ptr can point to any data type flag as an int type. eg int or int[ ]*/





ptr = the address of something.


%26amp;ptr = this is the address of the pointer ptr , in terms of memory storage


*ptr = this "dereferences" the pointer and returns the contence of the memory address contain in ptr.





for example.....





int* pointer; // at this point the pointer is swinging... if you deref it you will segfault this is because it contains a random address some where on your computer.





int number = 100;





// now we want to store the the memory address of "number" so we say. "%26amp;" mean "the address of"





pointer = %26amp;number





// now we want to out put the number that the point points to.


so we dereference the pointer... which mean that we look at the contence of the pointer and convert it to an address, then we move to the address location and return that data to the calling line.





cout %26lt;%26lt; *pointer %26lt;%26lt; endl;





// if we wish to alter the data begin referenced we can do this


// "*" means dereference. or n simple terms move to down the pointer to the thing being pointed at.





(*pointer)++;


//or


*pointer = *pointer + 1;








anyway i hope this helps


No comments:

Post a Comment