Pointers

So, based on what I’ve just read, a pointer is just another way of storeing information to call back to later.

Another way of storing information to call back to later… not quite, from what I understand.

A pointer holds the memory location of a piece of data. So when you assign a value to an int or double variable, the variable refers to the value but a pointer to the variable would refer to where that variable resides in memory. All data resides in memory somewhere. Before, we only cared about the value and not about the location of the data.

So:

int a = 5;
int* pointerToA = &a;

The value of variable a is 5, and this ignores where it is in memory.

Then we assign the address of a to a pointer called pointerToA. This holds the memory location of a. If we change the value of a, say with a = 6; the pointer will not change because it still popints to the same address in memory.

If we were to change the pointer to a, then it would point to somewhere else in memory entirely and who knows what the value there would be.

also, going along with what @mkkemp94 said. If you were to assign (by accident usually, lol), the value of a to pointerToA, it will point to the memory address location that you tell it to, and will most likely (though there is a chance it will be a valid memory address) give an access violation (since your program is usually only allowed to access a certain memory range), and low level memory addresses usually belong exclusively to your operating system kernel. If you keep getting access violations, check what you’re passing into a pointer.

Another neat tidbit, this is valid, but shows that anything can have a pointer to it, since a variable is plain and simply a memory address where data resides, even the pointer itself.
int a = 5;
int* ptrA = &a;
int** ptrPtrA = &ptrA;
int*** ptrPtrPtrA = &ptrPtrA;

This is one of the larger topics on this section; so to add to what others have already stated.

To avoid the ‘access violation error’ - you can create a blank pointer by setting it to “NULL.” so lets say we make.

I made a quick few files you can copy in visual studio to visualize what is happening; main:

#include "pch.h"
#include <iostream>
#include <bitset>
#include "Header.h"

int main()
{
		int referencetest = 3;

		pointtest pointertest;

		pointertest.setuppointer(referencetest);

		std::cout << "\nOur new value : " << referencetest;


		pointertest.pointtesting();


		std::cout << "\nOur new value : " << referencetest;

		std::cin >> referencetest;
        return 0;

}

header:

#pragma once
#include <iostream>
#include <string>

class pointtest
{
public:
	pointtest(); //constructor

	void setuppointer(int& settopoint);
	void pointtesting();

private:
	int* pointblank = NULL;

};

header.cpp:

#include "pch.h"
#include "Header.h"

pointtest::pointtest()
{
}

void pointtest::setuppointer(int& settopoint)
{
	pointblank = &settopoint;
	std::cout << "\nOur value that we refrence is :" << settopoint;

	std::cout << "\nOur address for the pointer is :" << std::hex << pointblank;
	// changing the value that we reference
	settopoint = 7;
	// changing value does not change address
	std::cout << "\nOur address for the pointer is :" << std::hex << pointblank;
	
	return;
}

void pointtest::pointtesting()
{
	std::cout << "\n" << pointblank;
	*pointblank = 3;
}

If you were to run this; notice that the pointer is initialized to “NULL” in the header file. Being private; this null pointer can now move around to our various class functions - but not changed from outside the class.

In the header.cpp I defined the function “setuppointer” to have the argument of a reference (int& settopoint). It then assigned the memory location of the variable to the private pointer; changes the value then returns.

So the big thing to take away here is that the reference is allowing you to change your variable (Be it a single integer; array; or anything you can send into another function) without making a copy of that variable (valuable time saver when dealing with huge amounts of data). The pointer is allowing us to point to the memory location of what it is set to; and also change it with the (*) dereference operator. This also allows us to work with the data without making a copy.

A reference can not be changed; a pointer can.

@McZawa - good examples but it is accepted practice to use nullptr now instead of NULL as the latter is ambiguous since it is a macro of int 0;

See this for more info: https://stackoverflow.com/questions/1282295/what-exactly-is-nullptr

Privacy & Terms