What is the Point of Pointer's

Discussion in 'C Programming' started by Bill Potter, Aug 4, 2004.

  1. Bill Potter

    Bill Potter Guest

    I am a learning programmer in C and i want to know why some one would use
    pointers instead of going direct!
    Bill Potter, Aug 4, 2004
    #1
    1. Advertising

  2. In article <>,
    Bill Potter <> wrote:

    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!


    The same reason why we use pointers in human languages, e.g.
    "home" instead of "17859 Main Street"
    "the car behind us" instead of "car with registration no KN5567YZ"
    "you" instead of "Bill Potter"

    --
    Göran Larsson http://www.mitt-eget.com/
    Goran Larsson, Aug 4, 2004
    #2
    1. Advertising

  3. Bill Potter wrote:

    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!
    >


    Let's say you're shopping for a car at a car dealership and the dealer
    asks you which car you like, what do you do?

    1. __Point__ at a couple of cars...

    or

    2. Go to each car, and physically move each of them directly in front
    of the dealer?


    Probably #1, if you're at all concerned about efficiency. You can refer
    to more cars quicker by pointing at them than moving them each directly
    in front of you, one by one.

    Does that help?


    Mark F. Haigh
    Mark F. Haigh, Aug 4, 2004
    #3
  4. Bill Potter

    Allan Bruce Guest

    "Bill Potter" <> wrote in message
    news:...
    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!
    >
    >


    for many reasons! 2 simple ones:

    1) when passing a large structure to a function, we dont want to copy every
    element and slow things down to a snail-pace. So pass a pointer instead,
    which is very quick.

    2) what if you want some information to be passed to a function and that
    function modifies some data. For example a sort function. We pass a
    pointer to an array to the function and let it do all the work.

    Basically, pointers speed things up. They also allow dynamic memory
    allocation using malloc() or equivalent. Things start to get more advanced
    when you have pointers to pointers ;-)
    Allan
    Allan Bruce, Aug 4, 2004
    #4
  5. Bill Potter

    osmium Guest

    Bill Potter writes:

    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!


    In C - as distinct from C++ - using a pointer is the *only* way a called
    function can alter the contents of a variable in the calling function.
    Parameters are passed in C in what is called "pass by value", that is, a
    *copy* is passed to the callee.
    osmium, Aug 4, 2004
    #5
  6. >> I am a learning programmer in C and i want to know why some one would use
    >> pointers instead of going direct!

    >
    >In C - as distinct from C++ - using a pointer is the *only* way a called
    >function can alter the contents of a variable in the calling function.
    >Parameters are passed in C in what is called "pass by value", that is, a
    >*copy* is passed to the callee.


    In fact, C++'s references could be taken as a syntactic modification of The
    Pointer, e.g.:

    C> void callee(int *ptr) {
    C> ++*ptr;
    C> }

    C++> void callee(int &ptr) {
    C++> ++ptr;
    C++> }


    Jan Engelhardt
    --
    Jan Engelhardt, Aug 4, 2004
    #6
  7. "Goran Larsson" <> wrote in message
    news:...
    > In article <>,
    > Bill Potter <> wrote:
    > > I am a learning programmer in C and i want to know why some one would

    use
    > > pointers instead of going direct!

    >
    > The same reason why we use pointers in human languages, e.g.
    > "home" instead of "17859 Main Street"
    > "the car behind us" instead of "car with registration no KN5567YZ"
    > "you" instead of "Bill Potter"


    Actually "17859 Main St" is a pointer as well; the house itself is not.
    Obviously every time you invite someone over, you'd rather give them a
    pointer to your house instead of building a copy and handing it to them.

    S

    --
    Stephen Sprunk "Those people who think they know everything
    CCIE #3723 are a great annoyance to those of us who do."
    K5SSS --Isaac Asimov
    Stephen Sprunk, Aug 4, 2004
    #7
  8. Bill Potter

    Default User Guest

    Bill Potter wrote:
    >
    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!



    Look into a typical linked list implementation.




    Brian Rodenborn
    Default User, Aug 4, 2004
    #8
  9. In article <U27Qc.33288$>,
    Stephen Sprunk <> wrote:

    > Actually "17859 Main St" is a pointer as well; the house itself is not.


    No. "17859 Main St" is the name of the house (variable).

    > Obviously every time you invite someone over, you'd rather give them a
    > pointer to your house instead of building a copy and handing it to them.


    No. You give them the name of the house. The problem starts when you
    move and they continue to visit the hose by its name. The pointer "home"
    should have been used instead.

    --
    Göran Larsson http://www.mitt-eget.com/
    Goran Larsson, Aug 4, 2004
    #9
  10. "Bill Potter" <> wrote in message news:<>...
    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!


    Pointers are direct. That's the point!
    Michael Scarlett, Aug 4, 2004
    #10
  11. Bill Potter wrote:

    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!


    Power!
    Wayne Rasmussen, Aug 4, 2004
    #11
  12. Bill Potter

    Default User Guest

    Goran Larsson wrote:
    >
    > In article <U27Qc.33288$>,
    > Stephen Sprunk <> wrote:
    >
    > > Actually "17859 Main St" is a pointer as well; the house itself is not.

    >
    > No. "17859 Main St" is the name of the house (variable).


    No, it's not, it's the address of the house. If you tore it down and
    built an office building called "the doctor's cooperative", it would
    still be at 17859 Main St.




    Brian Rodenborn
    Default User, Aug 4, 2004
    #12
  13. Bill Potter

    Mabden Guest

    "Default User" <> wrote in message
    news:...
    > Goran Larsson wrote:
    > >
    > > In article <U27Qc.33288$>,
    > > Stephen Sprunk <> wrote:
    > >
    > > > Actually "17859 Main St" is a pointer as well; the house itself is

    not.
    > >
    > > No. "17859 Main St" is the name of the house (variable).

    >
    > No, it's not, it's the address of the house. If you tore it down and
    > built an office building called "the doctor's cooperative", it would
    > still be at 17859 Main St.


    So you're saying the address of the house is the address of the house...?
    ;-)

    City planning guarantees that zero is never a valid address for a house...

    --
    Mabden
    Mabden, Aug 5, 2004
    #13
  14. Bill Potter

    kal Guest

    "Bill Potter" <> wrote in message news:<>...
    > I am a learning programmer in C and i want to know why
    > some one would use pointers instead of going direct!


    According to ak C there are a variety of pointers. Here is
    what they say why someone would like to have a G S Pointer.

    <OT>
    The Shorthair is friendly, intelligent, and willing to
    please. The first impression is that of a keen enthusiasm
    for work without indication of nervous or flightly character.
    </OT>
    kal, Aug 5, 2004
    #14
  15. Bill Potter

    red floyd Guest

    red floyd, Aug 5, 2004
    #15
  16. Bill Potter wrote:
    >
    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!


    Example:

    I wish to write a routine to return information about every device
    attached to the computer. The struct for the device information
    takes 128 bytes. There can be up to 128 devices attached to the
    computer, for a total of 16,384 bytes maximum.

    Should I pass a 16,384 byte buffer to the routine, and have the
    routine return that 16,384 bytes back to the caller? Or should
    I simply pass a pointer to that buffer?


    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Kenneth Brody, Aug 6, 2004
    #16
  17. Bill Potter wrote on 04/08/04 :
    > I am a learning programmer in C and i want to know why some one would use
    > pointers instead of going direct!


    What do you mean 'direct' ? Any object has an address. It's good to
    know its location if you want to access it. A pointer is just another
    variable that holds the address of an object. It's useful in many
    cases, for example if you want to 'pass' an array to a function.
    Actually, there is no choice, only the address of the array is passed
    into a pointer to the same type.


    int a[123] -> int *param_a

    char b[123] -> char *param_b

    Why? Probably for an question of efficiency. It could have been
    technically possible to pass a copy of the whole array, but it would
    have been highly unefficient.

    Note that pointers are not a C-feature. They belong to most
    architectures where some registers are designed to hold an address, and
    to reach the value via some indirection :

    [ES:DI]
    (A6)

    etc.

    The C 'sin' is to not hide this feature. Some other languages use
    different syntaxes to act as if the pointers were not existing,
    providing an extra abstraction layer. Is it good or not is debatable. I
    like C and its pointers, and I see no reason to use another language
    for now.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
    Emmanuel Delahaye, Aug 7, 2004
    #17
  18. Mabden wrote on 05/08/04 :
    >>> No. "17859 Main St" is the name of the house (variable).

    >>
    >> No, it's not, it's the address of the house. If you tore it down and
    >> built an office building called "the doctor's cooperative", it would
    >> still be at 17859 Main St.

    >
    > So you're saying the address of the house is the address of the house...?


    Don't mix pointer and address. A pointer is avariable. An address is
    the value of a pointer. An address is a pointer constant.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
    Emmanuel Delahaye, Aug 7, 2004
    #18
  19. Allan Bruce wrote on 04/08/04 :

    > 1) when passing a large structure to a function, we dont want to copy every
    > element and slow things down to a snail-pace. So pass a pointer instead,
    > which is very quick.


    This wording is confusing. Actually, you pass the address of the
    structure via a pointer of the same type. You dont 'pass a pointer'.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
    Emmanuel Delahaye, Aug 7, 2004
    #19
  20. Bill Potter

    Stefan Ram Guest

    Emmanuel Delahaye <> writes:
    >A pointer is avariable.


    ISO/IEC 9899:1999 (E) does not contain this assertion.

    In fact, "&x", for an appropriate variable x, has "pointer
    type". Which indicates that the value of "&x" is a pointer.
    And ISO/IEC 9899:1999 (E) indeed mentions the compound
    "pointer value", which is a pointer, but not necessarily an
    object (what you call "variable").
    Stefan Ram, Aug 7, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    697
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    837
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,249
    Fred Zwarts
    Jul 2, 2009
  4. Christopher
    Replies:
    4
    Views:
    440
    Ruben Safir
    Jul 9, 2011
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,322
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page