1

I'm having issues passing an array struct into a function for processing.

I think I need to pass the function the address of the array, however have got multiple compiler errors and am running out of combinations to try.

The function then needs to return a value to a new struct member.

Here is my best shot!

  //-----------------

  void Function(struct MyStruct* ptr);

  //------------------

  int main(){

  MyStruct array[MAX];

for (int i=0; i<MAX; i++)
{

    File>>array[i].V1;
    File>>array[i].V2;
    File>>array[i].V3;
    File>>array[i].V4;
    MyStruct* ptr = &array[i];
    array[i].V5 = Function(ptr);
}
  }

  //-----------------------

  void Function(struct MyStruct* ptr)
  {
  // do something with the struct, how to I access each element in here?
  }

Thanks!

1
  • Your main looks good. Don't forget to give Function a useful return type that'll match array[i].V5, and inside it you may access the members of MyStruct through the pointer like ptr->V1, ptr->V2 etc Commented Jan 24, 2012 at 0:57

1 Answer 1

1

You have a void type for Function, which is why you can't return anything. You should make it return whatever V5's type is instead.

Also, to access a element of ptr, use the arrow notation:

ptr->V5
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. Within my function: ptr->V5 = 1.0; gives error C2227: left of '->V5' must point to class/struct/union/generic type. This would save me having to define return types, which is what I want to do. Thanks!
@user1055774 How are you defining MyStruct? You should update your question to feature your full code.
Sorry, that's me. I had assigned a value that was of an incorrect type. The code is now working, sometimes you just need someone to point out the obvious. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.