   Newbie Question: Basic Pointer
  got this from "problem solving with c++ by "Walter Savitch". I''m wondering how this two pointer works.1. int *p, *p2 //will make p1=? p2=?2. P1=new int; //p1 & p2 will be ?3. *p1=42 //p1 will be 42 while p2 remains unchanged.4. p2=p1 //will both have a valu of 425. *p2=53 //both will have a value of 536. p1=new int //p1 will be ? while p2=537. *p1=88 //p1=88 p2=53my question is:1. in no.5, why are they both equal to 53 when p1 did''nt assign    assign a value of 53?2.in no.6 why p1 alone is unknown(?)?3.in no.7 why p1 alone has a value of 88?   No Signature 
  Because in 4 p2 was assigned the address of p1 (p2 = p1) then the value pointed to by p2 was assigned 53 (*p2 = 53) p2 and p1 both point to the same bit of memory therefore p1 and p2 are both 53.In 6 p1 is assigned a new piece of memory.  p2 still remains pointed at the same bit.  And finally because you are assigning the value addressed by p1 to 88 (*p1 = 88).If you refer to a pointer without the asterisk then you are referencing the memory it points to, if you use the asterisk then you are dereferencing the pointer, getting the value it points to.  Hope this helps ;  1 - p1 and p2 have the value of whatever was in the memory at the address the computer uses to store them. It is not proper to talk about *p1 nor *p2.2 - p1 will hold the address of a newly allocated memory block large enough to hold a single int. The value of the int is whatever was in memory at that address. p2 is unchanged3 - p1 still holds the same address. The int value at that address (*p1) is set to 42. p2 is unchanged.4 - p2 now holds the same address as p1. Therefore *p2 is 42.5 - The value at the address pointed by p2 (*p2) is now 53. Since p1 and p2 happen to point to the same memory location, *p1 is also 53.6 - p1 will hold the adress of another newly allocated memory block. The value of the int is once again undefined. p2 is unchanged, as is *p27 - p1, p2 and *p2 are unchanged, *p1 is 88.Answers1) in 5 p1 and p2 point to the same memory location. *p1 and *p2 are therefore just two identical ways of accessing it.2) Because you modified the address the pointer was pointing at, not the value at that address.3) Because p1 and p2 are no longer pointing to the same address. *p1 and *p2 are no longer aliases.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 19, 2004 4:05:16 AM];  one more question guys. Why is it that some of p1 and p2 have no pointer symbol? if I put pointer on each, let''s say instead of p1=p2, i''ll write it as *p1=*p2 will the porgram be the same if not, what difference will te program show? ;  p1 = p2will assign the address p2 contains to p1.*p1 = *p2will assign the value at the address p2 contains to the value at the address p1 contains.In other words the first example is assignment of the pointer itself, whereas the second example is assignment of the data pointed to.Was that your question? ;  ya, the asterix symbol when used with pointer variables is called a dereference.  read it like "at <variable name>".otherwise when you see a pointer variable without the asterix it is just a memory address of where whatever it is pointing to lives.  that''s why they call them "pointers" they "point" to a memory address where you can find what you''re looking for.i know this post is a bit redundant with the last one, it just deserves reiteration.-me ;  When learning pointers, I found it helpful to always remember this:A pointer is simply a variable which holds an address.  That''s it.  A char variable is a variable that holds a char.  An int variable is a variable that holds an integer.  A pointer variable is simply a variable that holds an address in memory.You can also try thinking of a pointer as a variable which points to another variable.  However, this kind of abstract thinking is sometimes difficult in the beginning stages of learning programming concepts.  I know it took me much longer to master pointers and references than it did the basic features of the language.int *p1, *p2;So if p1 and p2 are int pointers, they can point at any int variable.  The way a pointer "points" is by the address that they hold.  That is, p1 can contain an address of a variable and p2 can contain an address of a variable.  They can both contain the same address so they can both point at the same variable.To "point" a pointer to a variable you do this:int var1; // a simple integer variableint *p1; // a pointer to an integer, currently points at "nothing"p1 = var1; // now p1 points at var1Now remember, a pointer is just a variable that holds an address.  You can change the address that the pointer holds as much as you''d like:int var2, var3, var4;p1 = var2; // p1 now points at var2p1 = var3; // p1 now points at var3p1 = var4; // p1 now points at var4Now you''re thinking...what''s the point?  Well I agree this isn''t very useful unless you can actually get at the variable that the pointer is pointing at.  To do this, you have to use the dereferencing operator (*).int var5 = 8;p1 = var5; // p1 now points at var5cout << "The address of var5 is " << &var5 << endl;cout << "p1 holds address" << p1 << endl;cout << "p1 points to a variable with value " << *p1 << endl;The first cout line prints the address of the var5 variable (use the & to get the address of a variable).  The second cout line simply prints the value of the address that the pointer variable holds.  You will see that this is the same as the address of var5.The last cout line prints out the value of the variable that p1 points at.  Take some time to digest this and play around a little more with pointer variables before moving on in your journey to understand C/C++.Regards,Jeff    [ Little Devils Inc. ] ;  another question :when you say that the address is assigned, iep1=p2  //p1's address will now equal the address of p2well then , what does this mean? does it mean that if i didcout << p1;would it print out some sort of weird memory address? or would it print out a value???? also what if i did*p1 = &p2 //the value thats getting pointed to at p1 is now equal to the memory address of p2cout << p1;what about this? will this print out a value or a memory address?im just confused because i dont understand whats going on behind the scenes when you use an address of a pointer (p1) as opposed to the value pointed by the pointer (*p1)i understand an address can hold a value, but when we work with an address (p1) are we talking about the value at that address? or are we talking about the memory address itself?(weird long number)    thank you for any help you can give me!!!!   [edited by - graveyard filla on February 19, 2004 2:01:09 PM];  without the * symbol, it''s just a memory address.int *p1;int *p2;*p1 = &p2 will not compile in this case.  an int cannot hold a memory address unless you do a specific variable cast.this will work howeverint **p1;int *p2;*p1 = &p2in this case p1 is a pointer to a pointer to an int.  also called a handle to an int.  but don''t worry about this you''ll just get confusedso again:A POINTER IS JUST A MEMORY ADDRESS.say that 100 times until it sinks in.  pointers aren''t magic.  they are JUST addresses.  thats ALL.so given that, answer your own question:what will this print:cout << p1;if you understand that the value of p1 is just a memory address the answer should be obvious.again:A POINTER IS JUST A MEMORY ADDRESS.-me ;quote:Original post by Palidine  A POINTER IS JUST A MEMORY ADDRESS.Subtle correction:A POINTER IS JUST A VARIABLE THAT HOLDS A MEMORY ADDRESS.