• Advertisement
Sign in to follow this  

understanding pointers

This topic is 2561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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(B) and just use the pointer(A) without any * or & we get the memory address of pointer(B) assigned into Pointer(A).

when we dereference it once we get the memory address of the variable that pointer(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(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.

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement