.

Learning C++

<<

MatP

Newbie
Newbie

Posts: 37

Joined: Tue Jul 27, 2010 2:54 pm

Post Thu Aug 19, 2010 11:28 am

Learning C++

Hey :)

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!
<<

Xen

User avatar

Sr. Member
Sr. Member

Posts: 386

Joined: Tue Feb 03, 2009 3:59 am

Post Thu Aug 19, 2010 11:52 am

Re: Learning C++

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.
http://www.cprogramming.com/tutorial/lesson6.html
http://www.augustcouncil.com/~tgibson/tutorial/ptr.html
http://c-faq.com/ptrs/index.html
http://c-faq.com/malloc/index.html
http://c-faq.com/aryptr/index.html
<<

zeroflaw

User avatar

Full Member
Full Member

Posts: 208

Joined: Fri Feb 12, 2010 10:41 am

Location: Holland, Den Helder

Post Thu Aug 19, 2010 12:47 pm

Re: Learning C++

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.
ZF
<<

MatP

Newbie
Newbie

Posts: 37

Joined: Tue Jul 27, 2010 2:54 pm

Post Fri Aug 20, 2010 8:55 am

Re: Learning C++

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!

Thanks!
<<

Xen

User avatar

Sr. Member
Sr. Member

Posts: 386

Joined: Tue Feb 03, 2009 3:59 am

Post Sun Aug 22, 2010 7:59 am

Re: Learning C++

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
http://see.stanford.edu/see/lecturelist ... 8262e09e4e
http://www.youtube.com/user/stanforduni ... F856038C69

CS106-A Programming Methodology
http://see.stanford.edu/see/lecturelist ... 6adcae1111
http://www.youtube.com/user/stanforduni ... C7F4A1F852
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
http://see.stanford.edu/see/lecturelist ... 9d470755ee
http://www.youtube.com/user/stanforduni ... 49CA734A02

Edit: Just found a really good paper or Pointers. Do read it.
cslibrary.stanford.edu/102/PointersAndMemory.pdf
Last edited by Xen on Sun Aug 22, 2010 11:41 pm, edited 1 time in total.
<<

MatP

Newbie
Newbie

Posts: 37

Joined: Tue Jul 27, 2010 2:54 pm

Post Mon Aug 23, 2010 9:24 am

Re: Learning C++

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 :)
<<

MatP

Newbie
Newbie

Posts: 37

Joined: Tue Jul 27, 2010 2:54 pm

Post Mon Aug 23, 2010 1:09 pm

Re: Learning C++

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 :)
<<

Xen

User avatar

Sr. Member
Sr. Member

Posts: 386

Joined: Tue Feb 03, 2009 3:59 am

Post Mon Aug 23, 2010 1:15 pm

Re: Learning C++

Which book are you referencing?
<<

MatP

Newbie
Newbie

Posts: 37

Joined: Tue Jul 27, 2010 2:54 pm

Post Mon Aug 23, 2010 2:11 pm

Re: Learning C++


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron
.
Powered by phpBB® Forum Software © phpBB Group.
Designed by ST Software