Skip to content

Instantly share code, notes, and snippets.

@dmcleish91
Created September 28, 2016 15:12
Show Gist options
  • Select an option

  • Save dmcleish91/314d28f14b32c86c8f3910f21865ff2e to your computer and use it in GitHub Desktop.

Select an option

Save dmcleish91/314d28f14b32c86c8f3910f21865ff2e to your computer and use it in GitHub Desktop.

Revisions

  1. TheStygianSun created this gist Sep 28, 2016.
    20 changes: 20 additions & 0 deletions newtutorial.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #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;
    }