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( and just use the pointer(A) without any * or & we get the memory address of pointer( assigned into Pointer(A).
when we dereference it once we get the memory address of the variable that pointer( was pointing to and assign it into Pointer(A).
when we have a double dereference we get the value of the variable that pointer( 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.