Thread: Pointer

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    Pointer

    Hi!
    I have a little problem with POINTER.
    when should we use a pointer, and how dose it work?

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    A pointer is a type that "points" to another type. AKA an integer pointer points to the memory location of a certain integer. Therefore if you have

    int a = 9
    int* pa = &a <----- pa now points to the memory location of a.

    Now what are pointers good for ? There are plenty of things. I won't enumerate them all for you, but off the top of my head I can think of :

    1) Structure usage needs pointers.
    2) If you want to build arrays of arrays.
    3) If you want to send values to function and, if the values are changed within the function, you want to keep those values when the function returns.

    Hope that helps.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A pointer is just like any other variable. It holds a value. All variables hold values in C. That is to say:
    Code:
    int x;
    x = 10; /* put the value of 10 into the variable x */
    Pointers work exactly the same way, except the type of value they hold is different. They hold, as their value, the address of another variable. Just like the phone book stores your address.

    To get the address of a variable, you use the address-of operator. Like so:
    Code:
    int x;
    int *ptr;
    
    ptr = &x; /* Put the address of x into ptr. */
    This now makes ptr point to x.

    Commonly pointers are used to allow a function to change the value of something outside that function. Like so:
    Code:
    int x;
    
    ...
    
    foo( &x );
    
    ...
    
    void foo( int *n )
    {
        *n = 10;
    }
    Search the forums, or the like for a more detailed explanation of pointers. Or perhaps consult your favoured C book.

    I know I've written many an exasperatingly detailed pointer illustration here before, so if you're resourceful you should be able to find at least one of them around here some place.

    [edit] ......... Beat twice. [/edit]


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by Happy_Reaper
    1) Structure usage needs pointers.
    2) If you want to build arrays of arrays.
    3) If you want to send values to function and, if the values are
    and the most painful of them all: 4) linked lists.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    linked lists aren't painful. try red/black or avl trees

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM