Thread: Passing structure field to function

1. Passing structure field to function

Hi,

suppose I've a structures like this

Code:
```typedef struct
{
int f[5];
} foo;```
filled, for example:

Code:
```foo[0].f[0] = 1
foo[0].f[1] = 2
foo[0].f[2] = 3
foo[0].f[3] = 4
foo[0].f[4] = 5
foo[1].f[0] = 2
foo[1].f[1] = 4
foo[1].f[2] = 6
foo[1].f[3] = 8
foo[1].f[4] = 10
foo[2].f[0] = 3
foo[2].f[1] = 6
foo[2].f[2] = 9
foo[2].f[3] = 12
foo[2].f[4] = 15```
In this way, I have a table, how to pass to function column 1 (values 2,4,6) so I can use it as an array ?

Thanks.

2. You can:

a) Pass the array directly:

Code:
```
// ..
somefunction(foo[1].f); // prototype: somefunction(int *);

// ..```
b) Pass the structure as a pointer(Best method if you want to use other member of the struct)

Code:
```foo bar;

somefunction(&bar); // Prototype: somefunction(foo *bar);```
or c) Pass the struct directly - very inefficient so I won't even display code for that.

3. Thank you, I've edit my post, there's was an error!
I want column as array, not structure field.

4. You would have to pass the whole array (arrays are passed as pointers, so it's no biggie), then subscript through the data in the manner you want to.

5. There's no other way ?
So I've to duplicate function, one to accept struct and one to accept array.
I've some values into struct and other into arrays.

(I'm thinking that is not possible because I've no contiguos memory block but I can wrong)

6. What's the problem? Why won't a single function do for all, one that takes a struct or one that just takes an array?