My understanding of Pointers: By a coding newbie

First time coding in C++ and always been told it’s a “dangerous” language and you have to worry about garbage collection, memory allocation etc.

Anyway here is my summary of how pointers work.

If we assign a value to a variable, that specific variable is stored in a memory location on the computer. So lets say:

int myVar = 0;

However we have no idea where this variable has been stored. So we can get the memory location number as a value:

varLocation = &myVar;

now we have stored the memory address of myVar in varLocation by using the ‘&’ operator. Why would we want to do this? I believe it’s because we want the value to stay in the same memory location so that we’re not picking it up and dropping it into new memory locations which can create issues (I assume).

So instead of referencing myVar in our code, we instead want to reference varLocation and grab the variable inside of it.

*varLocation == myVar;

So we’ve used a dereference operator. We’ve found the memory location of myVar and extracted the value with ‘*’. Thus *varLocation is the same as myVar (the actual VALUE) where as varLocation == &myVar (the value ADDRESS).

However pointers do not inherently know the type of data they are dealing with, so we have to define what type it is pointing to.

int* varLocation;

So we have declare pointer values just like we declare regular values.

There is an inherit relationship between pointers and values.

int myVar1 = 5, myVar2 = 10;          //create 2 variables 5 and 10
int * var1Location, * var2Location;  //declare 2 pointers of type int

var1Location = &myVar1;    //assign location 1 to location of variable 1
var2Location = &myVar2;    //assign location 2 to location of variable 2

*var1Location + *var2Location == myVar1 + myVar2    //the addition of both pointers is the same as the addition of both
                                                    //variables whichs is 10 + 5 = 15

So lets just addume I have function that returns the value of a variable.

return(myVar1);    //returns 5
return(*var1Location);    //also returns 5

var1Location = var2Location;    // we've now changed the address of the pointer. 

return(myVar1);    //returns 5, because myVar1 is still sitting comfortably in it's memory allocation and hasn't changed.
return(*var1Location);    //returns 10! because now it is pointing to the memory address that is storing myVar2

*var1Location + *var2Location == 20;   //this statement is now true because it's just myVar2 * 2

So if we manipulate the memory adrress, the pointer is now pointing to different data in a different location.

ARRAYS. Just a quick note on pointers and arrays, it seems arrays store value in sequential order of memory allocation. so if myArray[0] is stored at location1 then myArray[1] is stored at location1 + 1

METAPOINTERS. So you can point a pointer to a pointer? if myLocation = &myVar works as we know, what if we do *myLocation = &myVar. Now the value in myLocation, is the same as myLocation!? so we can store a memory address in a variable, then point the variable inside the memory address which contains the memory address of different variable. Don’t know why we’d need this at this point.

Now as for the access operator, it seems fairly simple. Seeing as we can point to the address a function is stored, we may want use a method of the function.

Now my only question is are functions stored the same way?

As in are these the same things…

varLocation = &myVar    //assign location of myVar
*varLocation == myVar    // value stored in location is the same as myVar

int myFunc(int x){     //simple function returning an int + 1
    x = x+1;
    return(x);
}

funcLocation = &myFunc;    //assign a pointer to my Func
*funcLocation(x);    //would this act the same as myFunc(x)?

Actually the last 2 lines are wrong.

int (*funcLocation)() = myFunc    // so we assign the pointer directly to the function, without finding it's address.. i think

This is where my understanding breaks down.

Privacy & Terms