Thread: Pointers 101

  1. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Rep Power

    Pointers 101

    I worte this a while back to help me remember pointer stuff... thought it'd be nice to share with other newbies of my caliber..

    #include <stdio.h>
    *                Voodoo 'Doll' Petskull                        *
    *                            *
    *                          *
    *--pointers101.c--                                             *
    *-----------------                                             *
    *     Shooting for my 'Expert Pointers Merit Badge'            *
    *     Things to remember:                                      *
    *     '*' means 'Dereference' (Given a pointer, get the        *
    *                              thing referenced)               *
    *     '&' means 'Address of'  (Given a thing, point to it)     *
    *     ----------------------------------------------------     *
    *     thing       | Simple thing (variable)                    *
    *     &thing      | Pointer to variable 'thing'                *
    *     thing_ptr   | Pointer to an integer (may or may not be   *
    *                 |                        specific integer    *
    *                 |                       'thing')             *
    *     *thing_ptr  | Integer at the address that 'thing_ptr'    *
    *                 | points to                                  *
    *                                                              *
    int thing;        /* define a thing */
    int *thing_ptr;    /* define a pointer to a thing */
    int main(){
     thing = 14;       /* the box 'thing' now contains the value '14' */
     printf("The value of 'thing' is: %d\n", thing);
     thing_ptr = &thing;     /* Assigns the address(&) of 'thing' to the pointer 'thing_ptr' */
     *thing_ptr = 8;        /* thing_ptr points to the memory allocated to the variable 'thing',*/
                     /* so- by changing the value in that memory to '8'- 'thing' now equals '8' */
     printf("The value of 'thing' is now: %d\n", thing);
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Back in the real world.
    Rep Power
    i read that

    void func(*thing)


    void func(&thing)

    are NOT the same even though they behave the same. can anyone explain the actual difference? is this a pre-processor thing or what?

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Seattle WA
    Rep Power
    I think it's really just a matter of preference. Would you rather operate with thing being a pointer (hence requiring dereferencing, and -> operators), or treating it as just another variable? Both allow you to do the same thing (manipulate a variable with foreign scope), but each just differs the syntax of how you do it.

    Personally, when I prototype a function, if it's meant to take an object, I use pointers, MyFunction(MyClass* myThing). If it's a simple variable, then I use references, MyFunction(primitive_type& myVal).
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Woodland Hills, Los Angeles County, California, USA
    Rep Power
    Personally, I prefer func_by_pointer(foo *p); over func_by_reference(foo &p); in most cases. My reason is that when calling the functions, the syntax is:
    #include "myheader.h"
    int main(void) {
      int param = 5;
      func_by_pointer(& param);
      return 0;
    It is immediately obvious from the syntax, that the first function is accepting the address to the param and may change the value of the param. The syntax for the second function does not indicate that this function may actually alter the value of param and you wouldn't know it unless you actually looked up the declaration of func_by_reference() in myheader.h. Hence I prefer using pointer notation for functions like this. The only times I ever use reference notation is for copy constructors and operator overloading.

    Then again, this is a personal preference and I'd love to hear other views on the subject. :)
  8. #5
  9. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Rep Power
    ok, I'd just like to point out that I hate pointers....

    Not what they do, just dealing with them is a bitch...

    ...hence the snippet...

IMN logo majestic logo threadwatch logo seochat tools logo