cout<< *p <<"\n"; // when * comes first it will derefernce it meaning it will change the 0x654446 address to variable example=4,9,065,0 etc. u assigned x the address of p. when u change x the p will be changed.
Just now starting on pointers , and I has questionz.
Code:#include <iostream> using namespace std; int main() { int x; // A normal integer int *p; // A pointer to an integer p = &x; // Read it, "assign the address of x to p" cin>> x; // Put a value in x, we could also use *p here cin.ignore(); cout<< *p <<"\n"; // Note the use of the * to get the value cin.get(); }
Ok, I understand in the beginning int *p is declaring the pointer p, and the statement after that p = &x, is stating that the variable, not the pointer, p is holding the address for the variable x. Then when the cout statement comes up, it is outputting the value of x by checking the address to see what is stored there.....correct? I probably made that alot more difficult than it should've been.
cout<< *p <<"\n"; // when * comes first it will derefernce it meaning it will change the 0x654446 address to variable example=4,9,065,0 etc. u assigned x the address of p. when u change x the p will be changed.
*commence evil laugh* "MUWAHAHAHAHA!" *lightning strikes in background*
Oh. it will get a lot more complicated then that. But your absolutely correct. You did well for your first exposure to pointers. Except for this line... where I don't get what you mean:
"and the statement after that p = &x, is stating that the variable, not the pointer, p is holding the address for the variable x."
x ofcourse holds its own address, but so does p.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
p = the address of x
*p = the value of x
cp = copy paste(or child porn, but thats even worse) which is bad so never name your pointer cp
Ah we-a blaze the fyah, make it bun dem!
What Hell_demon said is what I meant. p=&x is assigning the address of x to p, but when you derefrence it(Thats the right term isn't it?) it give the value of x instead of the address.
Lawl, child porn as a variable name.
EDIT! Also, they gave a briefing of the keywords new and delete, and I think that it may very well kill me. Because at this point I have no idea about this "dynamic memory allocation"
Last edited by ilovecookies; 12-10-2009 at 01:41 PM.
Untested as I wrote it here.Code:int main() { int *p = new int; cout<<"2days random gibberish is: "<<p<<"and it contains the following value: "<<*p; system("pause"); // Only works for leet people delete p; //DELETE TEH LEET POINTER BECAUSE WE LIEK CLEAN CODENS NO? return 1337; }
Somehow I never understood why it always gave me the same address, maybe BA could explain that(probably because I did something wrong).
Ah we-a blaze the fyah, make it bun dem!