Results 1 to 6 of 6
  1. #1
    ilovecookies's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    In the C++ Section
    Posts
    321
    Reputation
    10
    Thanks
    67
    My Mood
    Shocked

    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.
    Quote Originally Posted by Jules Winnfield View Post
    I am the tyranny of evil men, and you are all the weak. But i'm trying Ringo,i'm trying real hard, to become the shepherd.
    excuse me miss, would you kindly reflect some photons off the epidermis covering your sternum directly into the camera iris or vacate the proximity immediately
    [IMG]https://i882.photobucke*****m/albums/ac23/miki_d420/RealizingYoureALeecher2copy.jpg[/IMG]









  2. #2
    Matrix_NEO006's Avatar
    Join Date
    Feb 2008
    Gender
    male
    Posts
    240
    Reputation
    12
    Thanks
    33
    My Mood
    Lonely
    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.

  3. #3
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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.

    "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

  4. #4
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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!

  5. #5
    ilovecookies's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    In the C++ Section
    Posts
    321
    Reputation
    10
    Thanks
    67
    My Mood
    Shocked
    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"
    Last edited by ilovecookies; 12-10-2009 at 01:41 PM.
    Quote Originally Posted by Jules Winnfield View Post
    I am the tyranny of evil men, and you are all the weak. But i'm trying Ringo,i'm trying real hard, to become the shepherd.
    excuse me miss, would you kindly reflect some photons off the epidermis covering your sternum directly into the camera iris or vacate the proximity immediately
    [IMG]https://i882.photobucke*****m/albums/ac23/miki_d420/RealizingYoureALeecher2copy.jpg[/IMG]









  6. #6
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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).
    Ah we-a blaze the fyah, make it bun dem!

Similar Threads

  1. [Question] Pointers & AA in C++
    By Hahaz in forum C++/C Programming
    Replies: 3
    Last Post: 07-09-2010, 01:31 PM
  2. Intro Movie Question
    By webhead24 in forum Combat Arms Mod Discussion
    Replies: 3
    Last Post: 01-25-2010, 12:25 PM
  3. pointer question
    By lowdownskin in forum Combat Arms Hacks & Cheats
    Replies: 2
    Last Post: 08-09-2008, 09:05 PM
  4. Question on Pointers
    By mains3rv3r in forum WarRock - International Hacks
    Replies: 8
    Last Post: 06-05-2007, 12:14 AM
  5. [QUESTION] Pointer in Dev-C++
    By yogilek in forum WarRock - International Hacks
    Replies: 0
    Last Post: 05-31-2007, 01:56 PM