This section is dedicated to anytime I create a quick reference quick to basic understanding of a topic in C++. All the information here can be found in two books. programming principles and practice using C++ by Bjarne Stroustrup and Data Structures and Algorithms in C++ by Michael T. Goodrich, Roberto Tamassia, David M. Mount
Memory
Computer memory is an incredibly complicated topic but as far as us programmers are concerned, memory is just a sequence of bytes. We can number the bytes from 0 to the last one. We call such a number that indicates a location in memory an address. We can think of an address as a kind of integer value.
int var = 15;
The above code will set aside an int-sized piece of memory for var somewhere in the available memory and put the value 17 into that memory.
Pointer
We can also store and manipulate addresses. An object that holds an address value is called a pointer.
int* ptr &var;
The * is used to indicate that we are using a pointer. the &(address of operator), is used to get the address of an object. So if var is stored at address 4096, ptr will hold a value of 4096.
char ch = 'c';
char* pc = &ch;
int ii = 13;
int* pi = ⅈ
std::cout<< *pc << *pi;
In both cases above we are defining a variable and then using the address of operator to store the actual memory address in the pointer. Next we use the dereference operator(*) to print out c and 13 to the console. The dereference operator is what we use to grab the actual value that a pointer is pointing to.
Important point
Even though a pointer value can be an integer, a pointer is not an integer. The question, what does an integer point to is not a well formed one. Integers do not point, pointers do. A pointer type provides the operations suitable for addresses, whereas, integers provides the arithmetic and logical operations suitable for integers.
Conclusion
Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
Latest comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Latest comments (0)