user1055774
user1055774

Reputation: 131

Basic C++ Function Pointers & Struct

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!

Upvotes: 1

Views: 345

Answers (1)

chrisaycock
chrisaycock

Reputation: 37928

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

Upvotes: 1

Related Questions