Results 1 to 10 of 10

Thread: Pointers

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Pointers

    Where can I find a good site about pointers? Documentation, tutorials, explainations, etc?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Okay. I have a basic understanding of what they do.

    I'm just confused as to why?

    Can anyone give me a practical example of using pointers?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Do a search round the forum, I explained this in detail a few months back

    Here's one use:
    Code:
    bool get_number(int key, int *target) {
        if(key > 20)
            return false;
    
        *target = raw_lookup(key);
        return true;
    }
    ...or something. Bit contrived, but it's a way of returning more than one value from a function. They have far more uses though...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Junior Member
    Join Date
    Oct 2002
    Location
    MI
    Posts
    23
    As far as i understand them, they let you play with variables that dont' belong to the function you're in.

    But i'm a complete newb, so take what i say with a grain of salt.

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    What's the difference between that and references then? like:

    Code:
    void myFunction(int &bing)
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    In this case references are C++'s answer to the problem pointers caused: bad pointers, ugly and hard-to-read syntax.

    references don't exist in C, so in C++ always use references unless you have a damn good reason to use pointers.

    There are many other uses to pointers, the most common are traversing arrays (including char arrays) and dynamic memory allocation.
    And a reference cannot be set to NULL, which sometimes can cause problems.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    hmm...

    I don't know why I find them so confusing, but I still don't have a damn clue.

    I'll just have to wait until they're covered in one of my classes.

    Thanks for your help!
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396

  9. #9
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    suppose you want the length of a null-terminated char array:

    This says: step thru each character, keep going as long as the char is not a zero. increment a counter for each non-zero char.

    Code:
    for(char *buf=somestring,int len=0;*buf;len++,buf++);
    At loop exit, len equals the number of characters.

    This is essentially what strlen() does.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Except that strlen will not increment a counter, but rather use pointer arithmetic:
    Code:
    size_t __cdecl strlen(const char* str) {
      const char *run;
      for(run=str;*run;++run);
      return run - str;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width