Thread: Pointers and references...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    29

    Question Pointers and references...

    I'm sure this has probably been asked before.. but I didn't find it in a forum search.

    What exactly is the difference between pointers and (I think references is the right term.. maybe not..) references?

    Just incase I got the references term wrong, what I mean is the difference between int &x and int *x.

    I understand a reference is like an alias of sorts to a variable, and a pointer can (does?) point to a place in memory, but i'm not sure what the real differences, when it comes to passing to functions, usage, etc. etc. are.

    Sorry for the newbie question, but I couldn't find any straight comparisons, and it would help me grasp the usage of both alittle better.

    Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    hey man...im a newbie with pointers as well, in fact, ive never worked with them yet. but i guess ur using stdio.h arent u???

    well, if so, "&x" means like "store the value in x"
    and "*x" means something like "asign address to x", where the address has a value, thou....

    im not sure of that, but thats what i undeerstand of it....

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    A pointer is a variable that holds an address. A reference acts as a constant pointer that is automatically de-referenced when used. The syntax is often cleaner when references are used, but as they lack flexibility, they are only useful for passing values into functions by reference and returning values from these functions(if the value returned is not created locally on the stack in the function).

    If you need to allocate memory dynamically, or need a variable that you use to walk through a section of memory then you need a pointer. Also it is sometimes clearer using pointers to pass variables into functions, if the function is to change the value, as it is clear when the variable is passed into the function that it's being passed by reference (as you have to use the & operator). Using references, it is not immediately clear to the caller that the value may be changed by the function.
    zen

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    29

    Smile Thanks

    Thanks to both of you, that generally clears things up for me.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    20

    Lightbulb pointers/references one more time

    Zen, as another newbie wrestling with the same question, can you confirm what I think you said:

    Here are 2 functions
    1. void fn(int& n, char& c) {n=3*n;}
    2. void fn(int *n, char *c) {*n=3*(*n);}
    both of these will change whatever I pass as n to 3*n. Please tell me that's right.

    But let me "wrestle" out loud with the rest of my problem: My Schaum's Outline makes a distinction between &/* as operators and type-modifiers which I interpret as follows:

    Operator:
    1. ptr = &n -- means that &n is an expression which evaluates to the address of n but leaves n unchanged.
    2. n=*ptr similarly sets n to the value ptr is pointing to without changing ptr.

    Type modifier:
    int& n; makes n a var of refers_to_an_int type
    int* n; makes n a var of point_to_an_int type

    Now assuming that the designers of C/C++ are smart, there must be a reason for using the same symbol for 2 different purposes, which is hopefully, that they are not different.
    Here's my shot at that:

    1. addressOf operator &n outputs an address which is obviously what you must pass to a function that is going to change n. This is why your description of "int& n;" as some kind of pointer rang a bell with me. An address should be/make a pointer. And since I like meaningful names, I would have included "pointer" in its name . . . maybe refPointer, or some such monster.

    2. dereference operator *ptr outputs a value whereas int* n creates a pointer. This makes no sense to me.
    First of all, shouldn't it be called the depointer operator? The only guess I can make as to their thinking is that if the first * (in the declaration) creates a pointer, it makes sense that the 2nd * (operator) would UNmake a pointer.

    Zen, you sound like you might be able to lead us all out of the wilderness, or at least confirm some of my suspicions.
    Thanx for your response,
    Al

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Here are 2 functions
    1. void fn(int& n, char& c) {n=3*n;}
    2. void fn(int *n, char *c) {*n=3*(*n);}
    both of these will change whatever I pass as n to 3*n. Please tell me that's right.
    Yes.

    Now assuming that the designers of C/C++ are smart, there must be a reason for using the same symbol for 2 different purposes, which is hopefully, that they are not different.
    It's either use more symbols to represent different things and prevent these symbols being used generally and potentially making the language confusing by using a large amount of operators, or use the same symbols to mean different (although similar) things which can also be confusing.

    An address should be/make a pointer. And since I like meaningful names, I would have included "pointer" in its name . . . maybe refPointer, or some such monster.
    Yes, but although they perform some of the same actions, they are still different so shouldn't be confused. A reference is more of an alias to a variable than a pointer. If you take the address of a reference you'll get the address of the variable it refers to whereas pointers have a unique location in memory. With references everything else is done behind the scenes.

    dereference operator *ptr outputs a value whereas int* n creates a pointer. This makes no sense to me.
    You have to differentiate between the use of a variable and it's declaration.

    First of all, shouldn't it be called the depointer operator?
    It could be but de-reference was used before there were references (in C). Also a pointer variable refers to another variable so semantically it's correct, but a bit confusing.
    zen

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    20
    Zen, Thanks. You let me learn and vent at the same time. What could be better.
    ciao

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Pointers or References?
    By leeor_net in forum C++ Programming
    Replies: 24
    Last Post: 02-04-2009, 02:29 PM
  2. Pointers and references?!? Help!
    By alyeska in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2008, 10:23 AM
  3. Pointers v References
    By m37h0d in forum C++ Programming
    Replies: 28
    Last Post: 06-30-2008, 01:29 PM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM