Question

When passing structures to functions what would the prototype / header look like? What would they look like when passing members of structures to functions?

For example...

struct a_struct{
int a;
int b;
};

a_struct point;

void f1(a_struct)
void f2(a_struct)

And lets say that I want to pass the whole structure to f1, but just a member to f2. Would I use the data type a_struct as parameter for both? Or would f2 have a different data type because I am only passing the member which is an int. Would this vary for an array of structures? The program I have been tasked to write is supposed to use arrays of structures. I figured that this wouldn't make much of a difference except that it will be passed by reference automatically.

Was it helpful?

Solution

When passing objects around (not just scalar values), you have to be concerned about memory ownership and copying, etc. This means that you have multiple options for how you declare the interface for your function. For example, you can pass by reference or pass by value.

A possible declaration for you might be:

void f1(a_struct& my_struct);

This would pass a reference to the a_struct object and prevent any copying of the object. However, your example structure just contains scalar values, so the possibility of copying the object isn't cause for too much worry. As a result, this would suffice:

void f1(a_struct my_struct);

As far as passing a member of the struct into a function, the function would need to be declared to take the type of the member. For example, to pass the a member of the a_struct into a function, you would do:

void f1(int val);

Finally, arrays do complicate things as they would come in as a pointer to the function. For example, to pass an array of a_struct objects, you would make this declaration:

void f1(a_struct* my_struct);

However, you could then simply reference the parameter normally. For example:

my_structs[1];

OTHER TIPS

For f1 to take an a_struct, it depends if you want to be able to edit it within f1. void f1(a_struct s); will work fine, and make a copy. void f1(const a_struct & s); takes a reference, but you can't change it (without some hassle anyway). I try to avoid non-const references.

If you want f2 to take an int, you write void f2(int);. It doesn't care where that int comes from (i.e. inside a struct or on its own). Then call it like f2(point.b);

Functions that take arrays can be troublesome, it depends what you want to do with it. The simplest way is to remember that an array degenerates to a pointer.

void f3 (a_struct * const structArray)
{ /* Do something */ }
a_struct myArray[10];
f3(myArray);

There are also other (better) ways to do this.

Edit: If you want to apply a function to every element of an array, there are (superficially) two approaches (which if you think about it, are only really one approach). Loop over the array, and pass each element to a function that takes an a_struct, or pass the whole array in (with a size parameter as well), and loop inside the function. I.e. you would do:

a_struct myArray[10];
for (int i = 0; i < 10; ++i)
{
    f1( myArray[i] );
}

// Or, better, from <algorithm>
std::for_each(myArray, myArray+10, f1);

Note this is not an out of bounds access. You are guaranteed to be allowed to make a pointer one off the end of an array (exactly for these looping situations), as long as it is never dereferenced.

Of course, this all assumes that you really do want an array, not a std::vector (in which case the loops look basically the same, except you would use the begin() and end() member functions).

In fact, if you have access to c++11 you can use std::begin() and std::end(), which will work exactly the same for vectors or arrays.

When you want to pass a structure (or a class):

return_type fn_name(object_name)

When you want to pass one of its members, let's say a in this case.

return_type fn_name(int)

Also, unless I am mistaken, all functions parameters default to pass by value unless you specify otherwise explicitly. If you need to pass arrays then it would be the same syntax as with any other types.

if you want to pass the value of just a member function send its type for example a function which takes 'a' of a_struct as paramaeter should be void func(int a);

but it will work for call by value. If you want to use call be reference use the pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top