How do u pass a dynamically created struct to a function by reference. I cant get this to work i keep getting error at the function call saying: " error: cannot convert 'Student**' to 'Student*' for argument '1' to 'void display(Student*) " ....... so can somebody take a look at this and help me fix it?
studentInfo isn't simply a pointer, it's a pointer to an array of structs. As such there's no need to pass it by reference, you can simply pass the pointer to the function. However, you have to declare the functions as void display (Student (*studentInfo)). At least, I'm pretty sure that's how you would declare it. ;) At the very least, the name of the array itself is converted to a pointer to the first element, so a pointer to an array is the same as a pointer to a pointer pointing to the first element, which is a struct, so it has the type of pointer to a pointer to a struct. As such, I'm also pretty sure you could declare the function as void display(Student **studentInfo) (note the lack of the array notation is this declaration.) Also, I'd use a different parameter name than that, since that was the name of the pointer you originally declared too. Not a big deal, it'll work, but it might make things a bit clearer in the function.
Also, in the function, since you are using a pointer, you can access the each structure that's pointer to using the arrow operator, ->. So you could write studentInfo->name. Then, to point to the next struct you can just increment the pointer.
Sure thing, however I might have been a bit wrong. Dynamically allocated arrays are a bit different iirc, so that studentInfo might simply be a pointer to a struct. If it was a normal array everything I said would be correct, but I'm not entirely sure it applies to a dynamically allocated one. Time to do some more reading hehe.
Edit: Actually, what was written in the other thread you wrote is correct. You can use studentInfo as the name as the array, no need to pass it by reference or alter your display function. It IS a pointer to the first element the way it is, so all you'd have to do is simply pass studentInfo to display without the & and it should work. Then you can use it just like an array with subscripts for each element, but also using either the member operator or the arrow operator.
Sorry about my mistake in the first post, arrays and pointers can be tricky things. :)
Just remove the ampersand from line 15. You don't need to pass display the address of the pointer. You need to pass it the value held by the pointer (which is the address of the dynamically allocated array.)
That's all that needs to be done to pass it correctly. If you wanted display to print each student in the array, you should pass the size of the array as well. And, it wouldn't hurt to actually assign something meaningful to the elements of the array before you "display" them.
void getFileInput(Student *);
Student *studentInfo; // Declaration for dynamically allocated array of structs
getInput(studentInfo); // Call getInput function and pass structure to it by reference
cout << studentInfo.id;
Get input function
void getInput(Student *stu)
stu = new Student[maxStu]; // Dynamically allocate an array of structs
The problem is that, inside getInput(), you're not only changing the contents of the memory that stu points to - you're changing the value of stu itself when you allocate the memory for it.
Since C++ passes function arguments by value, not by reference, this new value of stu is only available within the function - in main, the pointer will still have its old value - which, in the code you've posted is uninitialised.
You need to either:
1) Pass in a pointer to stu, i,e, Student **stuPtr
2) Pass in a reference to stu, i.e. Student &stu
3) Allocate the memory before calling the function, and pass in stu as you are doing now
Also, in main, you're tring to access the variable name stu, but it's not defined in main. Presumably you meant studentInfo?
You need to think more clearly about your types. You need to pass a reference to the thing you're trying to change the value of. You're not just trying to change a Student object. You're trying to change a pointer to a Student object. So you need your function to take a reference to the pointer.
I did a quick Google search for "Reference to a pointer C++" and the very first result was the following article: