3

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?

3
  • void yourFunc ( Register *param1) ....int main(void) { Register Reg[9]; yourFunc(Reg);} Commented Aug 9, 2016 at 12:17
  • would that keep its new values once returned to main? I would not need to return anything at the end of 'yourFunc'? Commented Aug 9, 2016 at 12:19
  • 1
    Yes the data pointed to by the pointer will not disappear when the function returns. Commented Aug 9, 2016 at 12:24

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nit: I feel that naming the pointer array "array" is misleading because it's not an array but a pointer.
3

In arrays decay to pointer when passed to function, so you can simply call the function passing your array, and that's it.

Little example using a pointer:

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>

typedef struct {
    int32_t TypeID;
    char InstrumentType[128];
} Register;


void func ( Register *reg, size_t n_regs )
{
    for (size_t i=0; i< n_regs; i++)
    {
        reg[i].TypeID = (int32_t)(i);
        sprintf(reg[i].InstrumentType, "Instrument_%zu", i);
    }
}

int main (void)
{
    Register Reg[9] = {{0}};

    func(Reg, sizeof(Reg)/sizeof(Reg[0]));

    for (size_t i=0; i<sizeof(Reg)/sizeof(Reg[0]); i++)
    {
        printf ("Reg[%zu].TypeID = %"PRId32"\n", i, Reg[i].TypeID);
        printf ("Reg[%zu].InstrumentType = %s\n", i, Reg[i].InstrumentType);
    }
}

As you can see you should also pass to the function you are going to write the size of passed array to be sure to not go out of bounds of passed array.

Comments

3

Arrays naturally decays to pointers to their first element. When you use an array you are actually using a pointer.

So the solution is simply to have the function accept a pointer to the structure as argument. It would also be helpful to provide the number of elements to the function, if it's not fixed and known beforehand.

It's important to know that using an array-like declaration for the function type, like

void TransferValues(Register regs[9])

or

void TransferValues(Register regs[])

doesn't actually declare regs as an array, the compiler silently converts regs to a pointer (Register *regs). This has implications when trying to use the sizeof trick to get the number of elements in the array.

For an actual array, like

Register regs[9]

you can use sizeof(regs) / sizeof(regs[0]) to get the number of elements in the array. This doesn't work in a function as all you have is a pointer, and doing sizeof on a pointer will give you the size of the actual pointer and not the data it points to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.