How to pass an element of a structure to another function??

P

Panda2

Say I have a structure such as:

struct Barn{
char type[10];
int number;
}animal[100]

so for example we might have data like
animal[0].type cow
animal[0].number 10

I want to pass this structure to another function where i might want to
do:

animal[0].number++
animal[1].number=5 (which say might be dogs... )

etc......

I'm really struggling to work out how to do this without making a
global declaration of the structure..

So, any thoughts on how to do this???
Thank-you
 
V

Victor Bazarov

Panda2 said:
Say I have a structure such as:

struct Barn{
char type[10];
int number;
}animal[100]

so for example we might have data like
animal[0].type cow
animal[0].number 10

I want to pass this structure to another function where i might want to
do:

animal[0].number++
animal[1].number=5 (which say might be dogs... )

etc......

I'm really struggling to work out how to do this without making a
global declaration of the structure..

So, any thoughts on how to do this???

You can pass an element of the 'animal' array by reference, you can pass
individual elements by reference, you can pass the entire array by reference
or by a pointer of the first element. It depends entirely on the design of
the function and what it's going to do and with what.

If you write that you "might want to do" animal[0].number++, etc., then you
probably are better off passing the entire array by reference:

void myfunction(Barn (&animal)[100])
{
animal[0].number++;
animal[1].number = 5;
}

What does your favourite book on C++ say about functions and argument
passing?
Or is that chapter totally written for geniuses and not for normal people?

V
 
P

Panda2

Thanks, I'll have a play around with passing the whole array the way
you suggested.

I don't have a book, been relying on online tutorials etc... and the
ones I have found don't seem to get to this...

I'm sure this would be easier if I had a book, easier still with a
teacher. Also, this whole pointer, reference stuff is rather
confusing.. As soon as I think I have it figured out, I realize I
don't, because the next thing you know someone is poking a pointer to a
function, and thats just silly now isn't it!

Maybe you do need to be some kind of genius...

Thanks
 
K

Karl Heinz Buchegger

Panda2 said:
Thanks, I'll have a play around with passing the whole array the way
you suggested.

I don't have a book, been relying on online tutorials etc... and the
ones I have found don't seem to get to this...

Forget online tutorials. Most of the regulars have never seen one
that is good enough for recommending.

Get a book.
See eg. http://ma.rtij.nl/acllc-c++.FAQ.html
 

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,189
Latest member
CryptoTaxSoftware

Latest Threads

Top