Jump to content

  • Log In with Google      Sign In   
  • Create Account


understanding pointers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 MagicalBearClaw   Members   -  Reputation: 252

Like
0Likes
Like

Posted 16 January 2011 - 03:21 PM

Hi, I'm a new user at gamedev.net but I've known about the site for a long time .

so im new to c++ but not new to programming i have program for a while in c#.

so I've been watching the software engineering videos at 3d buzz and just wanted to make sure i understand pointers before i continue on.

here's my code with comments on what i comprehended :

]#include <iostream>

using namespace std;

int main()
{
    int *ptr;	// initialize a pointer ptr of type int
    int x = 3;    // initialize a variable X of type int to 3
    ptr = &x;   // get the memory address of X by referencing it and assigning it to ptr

    cout << "&ptr = " << &ptr << endl;  // prints out the memory address of ptr
    cout << "ptr = " << ptr << endl;   // prints out ptr which has the memory address of X
    cout << "&x = " << &x << endl;    // prints out the memory address of X
    cout << "x = " << x << endl;    // prints the value of X
    cout << "*ptr = " << *ptr << endl;   // prints the dereference ptr which has the value of X
    cout << endl;

    int y = 23;  // initialize a variable y of type int to 23
    ptr = &y;   // get the memory address of Y by referencing it and assigning it to ptr
    int **dptr = &ptr;  // declare a double pointer dptr and assign the memory address of ptr

    cout << "&ptr = " << &ptr << endl;   // prints out the memory address of ptr
    cout << "ptr = " << ptr << endl;   // prints out ptr which has the memory address of Y
    cout << "&y = " << &y << endl;   // prints out the memory address of Y
    cout << "y = " << y << endl;   // prints the value of Y
    cout << "*ptr = " << *ptr << endl;   // prints the dereference ptr which has the value of Y
    cout << "dptr = " << dptr << endl;   // prints out dptr which has the memory address of ptr
    cout << "*dptr = " << *dptr << endl;   // prints the dereference dptr which has the memory address of Y assigned to it

    cout << "**dptr = " << **dptr << endl; // // prints the second(not sure of the word here) dereference of dptr which has the of Y

   /* so basically what i gathered is when you  have a & in front of a variable or pointer you get the memory  address as the value.

        when you have a pointer with a & and assign the reference of  a variable the pointer now holds the memory address of said variable.

        when we have a pointer without any * or & we get the memory address of the variable it was referencing.

        when you dereference a pointer with * you are gaining the value of said variable that was referenced.

        note:
        
        i like to think of dereferencing as decrypting the memory  address to gain access to the value of the variable (not sure if that  analogy is correct).

        when we have a double pointer (A) and assigning a reference to  an other pointer(<img src='http://public.gamedev.net/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' /> and just use the pointer(A) without any * or &  we get the memory address of pointer(<img src='http://public.gamedev.net/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' /> assigned into Pointer(A).

        when we dereference it once we get the memory address of the  variable that pointer(<img src='http://public.gamedev.net/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' /> was pointing to and assign it into Pointer(A).

        when we have a double dereference we get the value of the variable that pointer(<img src='http://public.gamedev.net/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' /> was pointing to and assign itinto Pointer(A).   */

    return 0;
}

so yes i know this may be a long read for some of you .

please let me know if I'm not correct or misunderstanding .

also i know that when reading it may sometimes that i have broken sentences as I'm not a good writer i tried to re-read everything to make sure its clear so if something is not clear let me know.

thank you for your time and help, its much appreciated.

Sponsor:

#2 PrestoChung   Members   -  Reputation: 177

Like
0Likes
Like

Posted 16 January 2011 - 06:33 PM

After reading through the code I don't notice any errors :)

#3 MagicalBearClaw   Members   -  Reputation: 252

Like
0Likes
Like

Posted 16 January 2011 - 07:09 PM

After reading through the code I don't notice any errors :)


i know their inst any errors just wanted to know if my understanding was correct of how pointers work

but thanks anyways

#4 PrestoChung   Members   -  Reputation: 177

Like
1Likes
Like

Posted 16 January 2011 - 07:13 PM

Right, I meant I don't see any errors with your reasoning, the comments describe accurately what is happening.

#5 MagicalBearClaw   Members   -  Reputation: 252

Like
0Likes
Like

Posted 16 January 2011 - 07:20 PM

Right, I meant I don't see any errors with your reasoning, the comments describe accurately what is happening.


oh thanks i though u meant errors in code

thanks i really appreciate it

#6 rip-off   Moderators   -  Reputation: 7651

Like
1Likes
Like

Posted 17 January 2011 - 04:17 AM

Please note that this forum is not a chat room. Type properly, including correct spelling, grammar and punctuation. Take your time, there is no rush. We don't expect 100% accuracy, no one can, but please make a general effort. Programmers in particular should pay attention to getting these right, the compiler will not forgive you if you omit semicolons or use different spellings for identifiers. Note that your signature contains a (repeated) spelling error, the quote is "evidence of absence", not "absents".

#7 typedef struct   Members   -  Reputation: 230

Like
0Likes
Like

Posted 17 January 2011 - 07:53 AM

It really bothers me that this is how classes teach pointers. Pointers are one of the hardest things for a new programmer (along with recursion). Write a linked list. Then write a tree. You'll understand pointers and recursion way better than the code you wrote, which makes pointers seem confusing and, ahem, pointless. I've been programming for 10 years and I've never used a pointer to an integer (excluding arrays of course)

EDIT: oh, and I disagree with your sig :)
Anthony Umfer




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS