Skip to content

Instantly share code, notes, and snippets.

@dmcleish91
Created September 28, 2016 15:12
Show Gist options
  • Save dmcleish91/314d28f14b32c86c8f3910f21865ff2e to your computer and use it in GitHub Desktop.
Save dmcleish91/314d28f14b32c86c8f3910f21865ff2e to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
void change(int* x) // essential we made a new pointer and pointed it to the same address as p
{
*x = 20; // this dereferences our pointer so we can change the variable. remember this is pointing to the memory on the heap
}
int main()
{
int* p = new int; // allocated new memory on the heap and gave it a pointer
*p = 10; // the derefereenced pointer is assigned to value 10
cout << p << " " << *p << "\n"; // this should print out the memory address and the value 10
change(p);
cout << p << " " << *p << "\n";
delete p; // this will delete the memory address stored here
system("pause"); // this is so we can read the console window
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment