At the moment I'm trying
void avg(everything)
But that gives me the error:
error: subscripted value is neither array nor pointer
And when I got this error earlier today it was because I wasn't passing a 2D array to the function properly. So I figure this is the same but I can't find the correct format to pass it in.
This is my typedef:
typedef struct structure
{
char names[13][9];
int scores[13][4];
float average[13];
char letter[13];
} stuff;
And this is my typedef array:
stuff everything[13];
A type introduced with
typedef
is an alias that can be used for a real type.For example:
Now you can use
some_type_name
instead ofstruct some_struct
.So when declaring a function which takes a structure of this "type" you use the type like any other type:
In
some_function
as defined above, you can usevar
like a normal structure variable.To define a function taking an array (or a pointer) to this type, that's equally simple:
In the function signature, you need to specify the type, not the specific name of a variable you want to pass in. Further, if you want to pass an array, you need to pass a pointer (you should probably be passing structs by pointers anyway, otherwise a copy of the data will be made each time you call the function). Hence you function should look like:
However, C arrays also have no concept of length. Hence, you should always pass in the length of the array to the function:
You'd then call this as follows:
Also, if the function doesn't modify the data in any way, you should signify this by specifying that the parameter is
const
: