Results 1 to 3 of 3

Thread: not understanding pointers

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2001
    Location
    Moving to Connecticut
    Posts
    26

    not understanding pointers

    I'm a complete newbie to C++, though I do know JavaScript, but I am clueless about understanding pointers. need some help please.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A pointer is a variable that contains a memory address. I'm assuming 32-bit now, because that's what you're most likely to be using (and 16-bit pointer methods really hurt).

    When you use the & operator on a variable, you get its memory location:
    Code:
    int iNum; /* An integer variable */
    int *ptr; /* A pointer to an integer variable */
    Notice at the moment, ptr doesn't refer to a valid location, and will contain a totally arbitrary number from somewhere in your memory. So, you have to initialise it:
    Code:
    ptr = &iNum;
    Now, ptr contains the memory location of iNum, rather than iNum's value. You can get iNum's value out of this by using the * operator (indirection/dereferencing):
    Code:
    int x = *ptr;
    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

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    What parksie wrote is just the basics of pointers. Pointers are a much more complex topic. There are pointers to functions, structs etc. Sometimes they are not easy to understand. I suggest you to find a good tutorial on pointers, read it and practice of course
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

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