Pointers

I am new to programming and am currently in college taking a C++ course. We have been going over pointers and I feel confused with them and what they are used for. I understand that pointers are used for a memory location ( I believe ) but am having a hard time finding out why they are used.

The way I see it is; pointers being locations saved in memory so I can use them in other functions that are not in the same class. It seems to me like pointers are variables that do not follow public:, private:, or protected: access spec. I understand that pointers can change variables from one function to another as well.

I have read the book and even asked the professor ( Which for some reason does not seem to know how to explain it ) to get better reasoning to pointers so I have basic understandings of concepts early.

What I am asking is for someone to help sort out my confusion and give me better reasoning to pointers so I can wrap my brain around the concept better. I am curious what they are used for, why they are used, and how are they used in situations.
pointers being locations saved in memory so I can use them in other functions that are not in the same class
A pointer is a variable. The value that a pointer takes is the address of another variable, or nullptr.

It seems to me like pointers are variables that do not follow public:, private:, or protected: access spec.
Access protection is a class (or struct) property. It controls the visibility of variables within class. It has nothing to do with the behaviour of variables, whether it be an int, double or pointer.

What I am asking is for someone to help sort out my confusion and give me better reasoning to pointers so I can wrap my brain around the concept better.
A good explanation of pointers is presented by Marshall Brain here. It's C, but the concept is the same. The explanation spans several pages.
http://computer.howstuffworks.com/c20.htm
I see where you're coming from. I didn't understand them much when I first learned of them, and thought they were counter-intuitive.

One big thing they're used for is dynamic memory allocation. Say you need to make an array, but you don' t know how big it needs to be until someone tells you while your program is running. You can use a pointer to dynamically allocate a new array at runtime. That pointer will point to the location of the array that you just created.

To try and keep it simple, pointers are basically just things that "point" to something else. They say "over there, that's where it is, and this is what type it is." They store the address of another variable, but they aren't actually objects of whatever they're pointing to. However, they DO know the TYPE of what they are pointing to. They can change what they're pointing to as well, as long as it is of the same type. They aren't completely blind, but the access protections of that class are still enforced. If it is a private member variable, the pointer cannot access it, since those can only be accessed from within the class or by friends.

They allow you to move around memory manually. I can declare a pointer, and then step through memory or go grab the address of anything that matches the type of that pointer.

Have you learned about abstract classes yet? You could make an abstract base class, and then create a derived class. You could instantiate the derived class, and then declare a base class pointer and make it point to the derived class instances. This is actually done all the time - I do this quite often in my own projects, especially to keep those interacting with my classes as blind as possible - there's no need for them to know anything more than absolutely necessary.
You can't instantiate an abstract class, but you sure can create a base class pointer. Hello Polymorphism, one of the defining features of C++, and one of the reasons why I use it so much. C++ allows such a high level of abstraction that so many other languages don't allow.

I'm guessing you might not have gotten there yet, so I'll stop there with that part of it.

Here's a good page on pointers: http://www.cplusplus.com/doc/tutorial/pointers/

These things are so useful. C/C++ wouldn't be what it is today without them.
Topic archived. No new replies allowed.