#include <iostream>
#include <cstdint>

int main()
{
    int32_t a = 47;
    printf("a is %d\n",a); // prints the value of a

    // (int32_t *) is a "pointer" type... it holds the location of an int32_t
    // & is how you get the location of something... so &a is the location of a in memory
    int32_t *p = &a;
    // again, "int32_t *" is the type and "p" is the variable

    printf("p is %p\n",p); // p is where a is stored in memory
    
    // NOTE: on operating systems with virtual memory (which is most of them)
    // the "location" of something in memory is only valid in that program...
    // "shared memory" where more than one program can refer to the same 
    // memory is an advanced topic.  The important thing to note is that
    // there is no relationship between the "location" (pointer value)
    // and the actual hardware on the computer.  It isn't like the 0 pointer
    // is the first byte of the first memory chip or anything like that... it
    // is more complex than that.
    
    // same as b=a ... in this context,
    // * means to "get the value at the location"
    int32_t b = *p; // be is a new variable at its own location
    printf("b is %d\n",b);
    
    // notice that assignment (=) of basic types always makes a copy
    *p = 4;
    printf("a is %d\n",a); // -> 4, because p is a's location
    printf("b is %d\n",b); // still 47, because b=*p made a copy of a
    
    int8_t s[7]="hi mom"; // for arrays, the variable name is already a pointer!
    printf("s is %p\n", s);
    printf("the first value in s is %c\n",s[0]);
    printf("the location of the first value in s is %p\n",&s[0]);
    printf("the location of the second value in s is %p\n",&s[1]);
    printf("the distance between 1st/2nd values is %d\n",&s[1]-&s[0]);
    
    int32_t i[2] = {1,2};
    printf("the location of the first value in i is %p\n",&i[0]);
    printf("the location of the second value in i is %p\n",&i[1]);
    printf("the distance between the 1st/2nd values is %d\n",&i[1]-&i[0]);
    
    // notice that the "distance" was 1 in both cases even though
    // the 2nd int32_t was 4 bytes later than the 1st... what is going on?
    //
    // Basically addition/subtraction of pointers is always in terms of the
    // number of items, not the number of bytes.  So the distance between
    // the 1st and 2nd uint32_1 is 1 uint32_t (which happens to be 4 bytes)
    // and the distance between the 1st and 2nd uint8_t is 1 uint8_t (1 byte).
    //
    // The lesson is... if you want to know the number of bytes between two
    // memory locations, or add a particular number of bytes to a memory location,
    // you have to know the "sizeof" the things being pointed to!
    
    // C++ also has another variable type called a "reference" variable:
    int32_t &ra = a;
    // this is not a new variable but just a synonym for a:
    printf("a is in memory at %p\n", &a);
    printf("ra is in memory at %p\n", &ra); // same
    
    // because reference variables are synonyms and have no storage space
    // of their own, the following would be illegal...
    // int32_t &ra;
    // ...because it does not refer to anything.
    
    // Don't get confused:
    // & can be either "take the address of" or "make a reference variable"
    // * can be either "get the value from address" or "make a pointer variable"
    
    int64_t arr[10];
    int64_t *p2 = arr;
    *(&p2[0]+1)=100; // make sure you understand this! it sets arr[1] to 100
    printf("arr[1] is %ld\n",arr[1]);
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: