Thread: Pointers 101

    #1
  1. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    12
    Rep Power
    0

    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..

    Code:
    #include <stdio.h>
    
    /***************************************************************
    *                Voodoo 'Doll' Petskull                        *
    *               petskull@twistedport.com                       *
    *              http://www.twistedport.com                      *
    *--------------------------------------------------------------*
    *--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);
    
     return(0);
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    i read that

    void func(*thing)

    and

    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?

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

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    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
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,641
    Rep Power
    4247
    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:
    Code:
    #include "myheader.h"
    int main(void) {
      int param = 5;
      func_by_pointer(& param);
      func_by_reference(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
    Posts
    12
    Rep Power
    0
    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