Need help witH C++ badly

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

haplo210

Thread Starter
Joined
Nov 23, 2004
Messages
997
Right im part of a second year EEE course at uni, one of the clases we do is C++.

Alot of folk are having problems, mainly because we dont understand alota stuff, bad lectures sometimes....no real idea. The main struggle is with pointers.

We know wht they are, a variable in essence which points to a memory location holoding a specific piece of data. But why use them? Why not just create a variable with the data insteda of a pointer?

Whats the advantage of pointers?

Thanks in advance.
 
Joined
Dec 3, 2004
Messages
119
There are many useful advantages to pointers. One of the big advantages is memory. If you declare a variable that is rather large and it has to be passed into several different functions, using pointers will allow you to pass in an address instead of declaring the variable local to each function, thus using more and more memory.

If you want to learn more about C++, try using the Internet. There are tons of websites that can help explain C++. Below are just a few:

http://www.research.att.com/~bs/C++.html
http://www.cprogramming.com/
http://www.hitmill.com/programming/dotNET/csharp.html#tutorials
http://www.bnetsupport.com/
 
Joined
Apr 30, 2001
Messages
2,636
Well there are lots of functions that only accept a pointer as an argument.

At least with things like std::string, you can use the c_str() member to return a pointer to the string.

Also, as already said, passing by reference instead of by value can yield better performance.

Also, if you ever use iterators to traverse a container, you'll notice that the iterator is a pointer to the position in the container. You can then deference the pointer to return the value you want.

Here are 2 array examples (although I avoid arrays like the plaque)

Code:
#include <iostream>

int main() {
    const char x[] = "abcdefgh";
    for (std::size_t i = 0; i < ( sizeof(x) / sizeof(x[0]) ) - 1 ; ++i) {
        std::cout << x[i] << std::endl;
    }
}
Code:
#include <iostream>

int main() {
    const char x[] = "abcdefgh";
    const char* ptr = x;
    do {
        std::cout << *ptr << "\n";
    }
    while (*++ptr);
}
They both do the same thing, but the first one is incrementing an unsigned int which is then using the value of the unsigned integer and [] to deference the pointer at each location to return the corresponding value.

The second is just incrementing the pointer directly and that can yield better performance.

Here's an example of traversing a vector using an iterator, which in this case is a pointer too. It basically works in the same way as the 2nd array example.

Code:
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> v;
    v.push_back("1");
    v.push_back("2");
    v.push_back("3");
    
    typedef std::vector<std::string>::const_iterator it;

    for (it i = v.begin(); i != v.end(); ++i ) {
        std::cout << *i << "\n";
    }
}
If you have a c++ string and you want to print out the value in each position, you'd be using a pointer. It's just whether you do it directly or indirectly.

Using pointers directly can make code simpler in addition to better performance, but like anything, it all depends on the situation.
 

haplo210

Thread Starter
Joined
Nov 23, 2004
Messages
997
Thanks for you help. Im using the "sams teach yourself C++ in 10 minute book" its an acompianment to my class at uni. Still confused but i have a bit better understanding now.

Thanks for the links too.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top