Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie question about pointers to arrays

Reply
Thread Tools

Newbie question about pointers to arrays

 
 
Cam
Guest
Posts: n/a
 
      04-23-2004
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



 
Reply With Quote
 
 
 
 
Marty
Guest
Posts: n/a
 
      04-23-2004

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      04-23-2004
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=%22Th...ora.net&rnum=1
http://www.google.com/groups?q=%22Th...ora.net&rnum=5
http://www.google.com/groups?q=%22Th...ra.net&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

 
Reply With Quote
 
Cam
Guest
Posts: n/a
 
      04-23-2004
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.

)

Cam


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
casting pointers/arrays to multidimensional arrays Francesco C++ 2 11-06-2009 09:04 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Learning pointers and arrays of pointers With Files ketema@gmail.com C Programming 1 03-28-2005 03:51 AM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments