pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fressanme
    New Member
    • Apr 2007
    • 36

    pointers

    can anyone give me a concise and an easy-to-understand discussion on pointers? ive been reading about pointers for months now and still i dont clearly understand how to use it..a short discussion and example will do..thanks!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Try this and see if it helps.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by fressanme
      a short discussion and example will do..thanks!
      Example:

      [code=c]
      char * cp;
      [/code]

      Short discussion:

      Oh my, I think I just saw a pointer out there!
      You did? No you didn't!
      Yes I did and it was a nice char(ming) pointer as well!
      No you did not!
      Yes I did!
      You did not!
      I did!
      Did not! It was an ugly pointer as they all are!
      Did!
      Did not!!!!
      Did! Did! Did!
      Dit not! A slimey pointer! Don't trust them!
      I did see it!
      Did not!
      Did!
      ...

      kind regards,

      Jos ;-)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You already know how to use pointers. You do it every day.

        Example: You are invited to a party at 123 Center Ave at 7pm.

        When you write this address on a piece of paper so you don't forget it, the paper becomes a pointer containing the address of a party. That is, the paper is a Party*. The actual party at that location (123 Center Ave), is a variable.

        So the procedure is:

        1) Create a piece of paper (the pointer variable)

        Party* ptr;

        2) Create a party variable:

        struct Party
        {
        char* whattobring;
        };

        Party Fri7pm;

        3) Write the address of the party on the paper:

        ptr = &Fri7pm;

        4) Determine what to bring indirectly using the pointer:

        ptr->whattobring

        OR:

        (*ptr).whattobr ing

        Orignially, C designed the syntax this way;

        int a, b, c;

        Here a, b, and c are ints.

        Then they did this:

        int a,b,c, *d;

        Here a,b,c and *d are ints.

        d is the pointer variable that is to contain the address of an int. *d is that int.

        You use pointers so you can determine what to do indirectly. Say to compare two struct variables. You write a function that takes two pointer arguments. That way the function can compare any two variables without needing to make a copy of the variables.

        You also use pointers so a function can change the contents of a variable that's not part of the function. You do this by passing in the address of the variable in a pointer. The function de-references the pointer and makes the change at the address contained in the pointer. Again, this avoids having to make a copy of the variable as a local variable, make the change in the local copy and then make a copy of the local copy to return.

        Comment

        Working...