Newbie question about pointers to arrays

C

Cam

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
 
M

Marty

Cam said:
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
 
T

Thomas Matthews

Cam said:
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 newsgroup
for: "The Rule" by Chris Torek
http://www.google.com/groups?q="The...8&[email protected]&rnum=1
http://www.google.com/groups?q="The...8&[email protected]&rnum=5
http://www.google.com/groups?q="The...&[email protected]&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
 
C

Cam

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top