0

I think it is about time I learn the difference. I have seen how to use these, but the tutorials have been kinda vague. I have a very basic knowledge on the syntax and how to use them to a very small extent. Pointers have always seemed kinda scary and confusing. I have heard once you learn to use them they are great, and I want something great :). So how can I use these and at what time should I use each one? One more thing, I am using C++.

Thank you!

retsgorf297
  • 319
  • 1
  • 2
  • 9
  • @MooingDuck I was not aware of this. – retsgorf297 Sep 11 '13 at 00:43
  • 1
    It was the #1 hit that showed on your screen of similar questions while you typed this, and is also on the sidebar to the right on this page, along with seven other duplicate questions. I understand wanting to ask a question, but please read the things on your screen :) – Mooing Duck Sep 11 '13 at 00:44
  • Please don't beat me up over having a possible duplicate. I am 13, and I guess I am not that good at research. This my 2nd attempt at this site. I really like this so please don't keep me from asking questions on here. – retsgorf297 Sep 11 '13 at 00:45
  • @MooingDuck I did look at that. I even searched this site for it. I guess I used the wrong words, sorry. – retsgorf297 Sep 11 '13 at 00:46
  • No worries, just be sure that next time you write a question, look at the suggested duplicates that it shows on the screen before you hit the submit button. If you post too many duplicate questions, the site automatically blocks you. :( – Mooing Duck Sep 11 '13 at 00:50
  • @MooingDuck That has happened to me on my previous account. That is what I was scared of. – retsgorf297 Sep 11 '13 at 00:53

1 Answers1

1

Even though I agree with Mooing Duck here a small sample that might shed light:

int nValue = 5;
/* 
 * &rfValue is a reference type, and the & means reference to.
 * references must be given a value upon decleration.
 * (shortcut like behaviour)
 * it's better to use reference type when referencing a valriable,
 * since it always has to point to a valid object it can never
 * point to a null memory. 
 */
int &rfValue = nValue;

/* This is wrong! reference must point to a value. it cannot be
 * null.
 */
//int &rfOtherValue; /* wrong!*/

/* same effect as above. It's a const pointer, meaning pValue 
 * cannot point to a different value after initialization.
 */
int *const pValue = &nValue; //same as above

rfValue++;  //nValue and rfValue is 6
nValue++;   //nValue and rfValue is 7
cout << rfValue << " & " << *pValue << " should be 7" << endl;  
ArmenB
  • 2,125
  • 3
  • 23
  • 47