Sign in to follow this  

Shameful noob C++ question

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been programming in C an Java for a while, and am just reading through a tute on C++ so most stuff is fine. One thing I dont get, however, is the difference between references in C++ and pointers in C when it comes to passing arguments to functions. In C (pointer) int x = 5; y = func(&x); where func prototype would be: int func(int*) In C++ (reference) int x = 5; y = func(x); where func prototype would be: int func(int&) Other than a notational difference, is there any difference between these methods? Can I still do the C method in C++ (I could test that out in a minute)?

Share this post


Link to post
Share on other sites
Well pointers are more like a special kind of structure with * & -> operators for accessing where they point. References can be linked to one single object upon initilisation & you basically treat them asif they were that object. Also they attach only by reference (value) so passing the address would be no good.

char message[]="testing";
reference(message);
pointer(message);

void reference(char &character){
++character; // would increment the value. result: "uesting"
}
void pointer(char *character){
++character; //would increment the pointing address of this local pointer
}

You cannot edit where a reference goes once it has been linked because it just that - a link. infact most compilers should substitute it for the variable itself when it's compiled, so no new variable is created & no copying occours like a normal pointer. It's good that you're interested in references. use them whenever you can, they totally rule ;]

Share this post


Link to post
Share on other sites
Even though this problem appears to be solved, I just thought I'd step in and offer a little pointer. (pun intended [smile])

There is NO SUCH THING as a 'shameful noob question', especially not when it's regarding C++. EVERYONE was a newbie at one point, and when you don't know something, you don't know something, and there is no shame in asking for help, especially with something so complicated as a programming language. Heaven knows I did a lot of that [grin]

Cheers! If you ever have problems, don't hesitate to ask (provided you can't find the answers on your own of course)!

Share this post


Link to post
Share on other sites
Since SirLuthor commented on this, one other even much more important thing about the topic title is that it doesn't say anything. Topic titles exist to describe the problem, so it's more easy for people to decide if they want to read it or not. "N00b problem" doesn't describe anything, we already know you have a problem. You should name your topics like "Differences between C++ pointers&references" or something like that. Read the FAQ for more infos about how to make good posts.

Share this post


Link to post
Share on other sites
And in addition to what mikeman said, you posted in the "beginner"-section, so there is no need to repeat that in your thread-title. Instead you should make it as easy as possible for those kind souls who are to help you by naming your thread something descriptive. :)

Share this post


Link to post
Share on other sites
I have been posting here for a while now and usually I am in the graphics programming theory section. The reason why I gave the thread the title I did was because I have been programming for years doing quite complex stuff and this is something that i really should have known by now, so its a bit embarrasing that I didnt.

Other than that, yes I agree the thread should have been better named, i usually do use sensible titles, but I will be more careful next time.

Share this post


Link to post
Share on other sites

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this