August 19, 2010 at 4:28 pm #5486
So I’m in the process of learning C++. I have a small problem though.
I find it difficult at times to understand the point of pointers. Perhaps it’s simply the textbook I am using doesn’t provide a good explanation or a good example. I just can’t think of any reason I’d want to use them in a program I was creating myself.
Now I’m sure there must be plenty of reasons out there as I’ve seen them in examples of other code so I’m wondering if anyone has a good resource to teach this aspect of programming or could provide an example problem that requires a solution using pointers?
Thanks for the help!
August 19, 2010 at 4:52 pm #34699
Have you ever tried to pass an array as an argument to a function? How do you do this?<– Think on this for a moment, and try to find a solution for it.
Also, how do you allocate dynamic memory?
I learned C programming (I’m not very good at C++ though I can read and understand the code) and especially pointers from a lot of books and online articles, so I can’t direct you to any absolute resource. However, a good starting point will be to clear your concepts pertaining to arrays and strings and how they’re stored in memory and then move to pointer. Learn all about pointers,pointers to arrays/strings, array of pointers (different form pointer to arrays), function pointers, pointers to other datatypes and try to compare how using a pointer is different from using that variable in a normal way. At each point try to visualize what’s happening in the memory.
Here are some resource which might help you.
August 19, 2010 at 5:47 pm #34700zeroflawParticipant
With pointers you have full control over the memory in your program. Also sometimes you need to work with memory addresses, they can be stored in pointers.
And like Equix3n said, how do you allocate dynamic memory? Or how do you access newly allocated memory? You need pointers for this. It’s also needed when implementing polymorphism in your program. You have different types of objects, and sometimes you don’t know the type in advance or want to change it. You can have your pointer point to a different type of object then.
There are also lots of functions from different API’s that need pointers as arguments.
August 20, 2010 at 1:55 pm #34701
Thanks for the help guys 🙂 I appreciate it. I will definitely take a look at those links over the next few days.
I hadn’t considered a lot of what you’ve given me. I’m still fairly new to C++ (and programming in general, haven’t done anything beyond beginner stuff before) so there are a number of concepts still quite foreign to me. Anyway, I will continue reading/learning!
August 22, 2010 at 12:59 pm #34702
You might also want to consider the following free lectures from Stanford.
CS106-B Programming Abstractions: Teaches C++ programming. Natural successor to CS106-A which teaches programming methodology in Java
CS106-A Programming Methodology
I haven’t gone through both of the above, but I checked the first video of CS106A and I guess you’ll be fine if you jump straight into CS106B.
I’m currently going through CS107. Excellent stuff. It’s a must if you want to learn some advanced programming basics. Highly recommended. It basically teaches what happens under the hood, so don’t study from it if don’t already know basic C/C++.
CS107 Programming Paradigms
Edit: Just found a really good paper or Pointers. Do read it.
August 23, 2010 at 2:24 pm #34703
Awesome, I will definitely look over all of those videos/the paper linked when I’m at home and have some time. Thanks for the links and all the resources you’ve provided so far. Slowly but surely I’ll figure this stuff out and get into more advanced concepts 🙂
August 23, 2010 at 6:09 pm #34704
So I’ve read over some of the content you linked in your first post Equix3n- and I’ve got to say that the text book I’m reading, which has been mostly pretty good, is really terrible at explaining the concept of pointers.
Thanks so much for your links 🙂
August 23, 2010 at 6:15 pm #34705
Which book are you referencing?
August 23, 2010 at 7:11 pm #34706
- You must be logged in to reply to this topic.