简体   繁体   中英

passing an array of structs to a function in c++

I have already written a program that in functioning properly. I need to break it off into functions. I have 3 arrays of structs. I would like to make a function that reads the information from the file and echoprints it out. I just need an example on how I would pass it. I would post my code but I do not want the other students to take it. Thanks.

If you are using C arrays:

struct A { int v; }
A data[10];

void func(A *array, size_t n) {
}

func(data, 10);

Or if you are using a vector:

std::vector<A> vec;

void func(std::vector<A>& array) {
}

func(vec);

Since you are tagging this as "C++", I assume you are using vectors ( :-) )

void f1 ( yourVector& yourVector )
{
  // do something with the vector as read-write (for example fill the vector with somthing).
}


void f2 ( const yourVector& yourVector )
{
  // do something with the vector as read-only.
}

int main()
{
  std::vector <yourStruct> yourVector;
  f1( yourVector );
  f2( yourVector );
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM