Question on intro to pointers

Posts 1–6 of 6 · Page 1 of 1
Question on intro to pointers
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.
Quote Originally Posted by ilovecookies View Post
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.
*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.
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
Quote Originally Posted by why06 View Post
*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.
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.

Quote Originally Posted by Hell_Demon View Post
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
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"
Quote Originally Posted by ilovecookies View Post
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"
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;
}
Untested as I wrote it here.
Somehow I never understood why it always gave me the same address, maybe BA could explain that(probably because I did something wrong).
Posts 1–6 of 6 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us