carrayspointersstructmember-access

C: How do I correctly access an Array element of a Struct passed by reference?


I have this code:

typedef struct _structVar{
   int myArray[10];
} structVar;

structVar MyStruct;

Then I'm passing the struct by reference to a function:

myFunct(&MyStruct);

How I access array elements inside MyFunct?

void myFunct(structVar *iStruct){

   for(char i=0; i<10; i++)
      (*iStruct)->myArray[i] = i; //Fix Here
}

Thanks for the help.


Solution

  • Either write

    iStruct->myArray[i] = i;
    

    or

    (*iStruct).myArray[i] = i;