0

I was wondering how to pass a struct array by reference into a function. I found how to do it with pointers but wanted to learn with references too. Here is my code thus far.

struct employeeType
{
string firstName;
...
double monthlyBonus;
};

void readEmpData(ifstream& infile, employeeType *emp, int length);

I thought I just did employeeType& emp or employeeType& emp[] but get errors and everything I have googled just did pointers.

Here is the full code at pastebin for clarification for my learning experiment: http://pastebin.com/6NfZ3LC4

2
  • Already answered here:stackoverflow.com/questions/1106957/… Commented Sep 16, 2011 at 2:30
  • @Daniel, this might be different. It looks like he is trying to return an array. Commented Sep 16, 2011 at 2:41

3 Answers 3

3

Is the array of a fixed size? If not, you could use a template:

template <unsigned int N>
void myFunction(Foo (&arr)[N])
{
   // arr[0] etc.
}

Naturally, this will only work for compile-time arrays:

void f()
{
  Foo a[10];
  myFunction(a);  // OK

  Foo * b = new Foo[11];
  myFunction(b); // error, nonsense, b is a pointer, not an array
}

(If your array is always of the same size, you can skip the template and put the size into the argument directly. Note that "array" isn't one type in C++, but rather, T[N] is a different type for each N. That's why you cannot have a single function for "all arrays".)

Sign up to request clarification or add additional context in comments.

Comments

0

I assume you want readEmpData to read in an array of employees from a file. In this case, the caller wouldn't know how many employees there are and so the array and its length should be output parameters. An appropriate signature would be:

void readEmpData(ifstream & infile, employeeType *& emp, int & length);

or

employeeType * readEmpData(ifstream & infile, int & length);

You could also define the operator << for employeeType and read using STL:

vector<employeeType> employees;
copy(istream_iterator(file), istream_iterator(), back_inserter(employees));

1 Comment

I tried the employeeType *& emp and got errors for it. I just stuck with *emp to fix it. I made comment notes for the other methods and everyone else's methods after trying them for future reference.
0

A pointer can be a pointer to a single object, or to an array of objects. Makes no difference. The reason you haven't found any references to arrays is that it's usually redundant.

*emp will access a single object, or the first object of an array.

emp[0] is exactly the same as *emp with a different syntax. They both behave exactly the same.

emp[1] will get the second object in an array. How does C++ know you passed an array to the function? It doesn't. It expects you to keep track of whether the pointer points to an array, and the size of the array.

2 Comments

Learning redundant things is still useful. Never know when you will get stuck with a boss that prefers redundancy.
@Clayton, I wasn't trying to make a value judgement, merely suggesting why you didn't find much material on the subject. Usually when there are two different ways of doing something people will gravitate to the simpler one, especially when the simpler way was the only way in C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.