-1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Eventine
  • 33
  • 4
  • 1
    just `iStruct->myArray[i] = i;` will do. – Blaze Jul 22 '19 at 09:58
  • `*iStruct` is the structure object itself, not a pointer to the structure. The "arrow" operator `->` only operates on pointers (it implies dereference). – Some programmer dude Jul 22 '19 at 09:58
  • 1
    And please remember that C doesn't have pass-by-reference at all, only pass-by-value. You pass the pointer by value, which in a way can be seen as *emulating* pass by reference, but it's still not proper reference passing. – Some programmer dude Jul 22 '19 at 10:00
  • @Someprogrammerdude: Emulating pass by reference is pass by reference. C++ adopted a built-in pass-by-reference, but that does not change the fact that if you pass by value a thing X that refers to a thing Y by pointing to it, you have passed a reference to Y. – Eric Postpischil Jul 22 '19 at 10:51

1 Answers1

5

Either write

iStruct->myArray[i] = i;

or

(*iStruct).myArray[i] = i;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335