Newbie question about pointers to arrays

Discussion in 'C++' started by Cam, Apr 23, 2004.

  1. Cam

    Cam Guest

    Hi everyone,

    I am having trouble grasping the different syntax of pointers being used to
    pass arrays between functions.

    Am I right with the following:

    ....
    int dog[8] // declares an 8 element array called dog whose elements are int
    int *dog_ptr = dog // declares a pointer called dog_ptr that points to the
    whole dog array
    ....
    dog_ptr[3] // refers to the fourth element (0, 1, 2, 3) of the dog array

    I believe that I have the third (dog_ptr[3]) line understood, but I cannot
    understand how I can use a pointer to point to the whole array instead of
    using a "for" loop to modify the whole array as individual elements.

    I would really appreciate a fairly simple explanation of how I should use
    these.

    Thanks,

    Cam
     
    Cam, Apr 23, 2004
    #1
    1. Advertising

  2. Cam

    Marty Guest

    "Cam" <retsigerymmudathotmaildotcom> wrote in message
    news:...
    > Hi everyone,
    >
    > I am having trouble grasping the different syntax of pointers being used

    to
    > pass arrays between functions.
    >
    > Am I right with the following:
    >
    > ...
    > int dog[8] // declares an 8 element array called dog whose elements are

    int
    > int *dog_ptr = dog // declares a pointer called dog_ptr that points to the
    > whole dog array
    > ...
    > dog_ptr[3] // refers to the fourth element (0, 1, 2, 3) of the dog array
    >
    > I believe that I have the third (dog_ptr[3]) line understood, but I cannot
    > understand how I can use a pointer to point to the whole array instead of
    > using a "for" loop to modify the whole array as individual elements.
    >
    > I would really appreciate a fairly simple explanation of how I should use
    > these.
    >
    > Thanks,
    >
    > Cam
    >


    int *dog_ptr = dog points to an int at address of dog (implied as dog is
    array). Not to whole array, as dog is array of ints, a pointer to an int can
    only look at one member at a time. The first one in this case I think
     
    Marty, Apr 23, 2004
    #2
    1. Advertising

  3. Cam wrote:
    > Hi everyone,
    >
    > I am having trouble grasping the different syntax of pointers being used to
    > pass arrays between functions.
    >
    > Am I right with the following:
    >
    > ...
    > int dog[8] // declares an 8 element array called dog whose elements are int
    > int *dog_ptr = dog // declares a pointer called dog_ptr that points to the
    > whole dog array

    No. The pointer dog_ptr points to the first element in the dog array.

    > ...
    > dog_ptr[3] // refers to the fourth element (0, 1, 2, 3) of the dog array

    Yes.

    >
    > I believe that I have the third (dog_ptr[3]) line understood, but I cannot
    > understand how I can use a pointer to point to the whole array instead of
    > using a "for" loop to modify the whole array as individual elements.
    >
    > I would really appreciate a fairly simple explanation of how I should use
    > these.
    >
    > Thanks,
    >
    > Cam


    There are many explanations about arrays:
    http://www.eskimo.com/~scs/c-faq/s6.html
    http://www.parashift.com/c -faq-lite/references.html

    The following were found by searching the news:comp.lang.c newsgroup
    for: "The Rule" by Chris Torek
    http://www.google.com/groups?q="The...8&selm=&rnum=1
    http://www.google.com/groups?q="The...8&selm=&rnum=5
    http://www.google.com/groups?q="The...&selm=&rnum=10

    Reading the FAQs and searching the newsgroups before posting
    is always an excellent idea.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Apr 23, 2004
    #3
  4. Cam

    Cam Guest

    Thanks for both answers.

    I do try to do a search for answers before posting questions although I must
    admit I haven't looked through the FAQ. I usually find that the replies in
    NG are clearer and easier to understand. There is also the opportunity to
    clear up any points if a response is hard to understand.

    :eek:)

    Cam
     
    Cam, Apr 23, 2004
    #4
    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. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    407
  2. Replies:
    1
    Views:
    682
    -berlin.de
    Mar 28, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,190
    Philipp
    Jan 20, 2009
  4. Francesco
    Replies:
    2
    Views:
    1,173
    Francesco
    Nov 6, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    739
Loading...

Share This Page