I have a structure "Register" declared like this:
typedef struct {
int TypeID;
unsigned char InstrumentType[128];
unsigned char RegTag[128];
unsigned char Protocol[128];
int RegNum;
unsigned char RW[128];
unsigned char RegisterType[128];
unsigned char Signed[128];
unsigned char Inverted[128];
unsigned char DataType[128];
int Counts;
} Register;
I have an array of Registers called "Reg[9]" and want to create a function called 'TransferValues' to assign values to all the fields in the structure for each element of the array. Once the values are updated they will be outputted individually in main(). How can I pass the array to and from this function?
You pass Reg into that function using the syntax TransferValues(Reg);
The function will be of the form
/*return type*/ TransferValues(Register* array)
(In the function body, you can use array[i] in place of *(array + i), where i is a valid index of the array, which can be more readable.)
In C, arrays decay to pointers when they are passed to functions. Note that you might want to also pass the number of elements in the array to the function too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With