How do you pass a dyanamically created array struct to a function

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Student
{
	string id;
	string name;
	double grades[MAX_GRADES];
	double average;
};

void display(Student *);    // function prototype

int main()
{
   Student *studentInfo = new Student[10];;
	
   display(&studentInfo);    // Call display function and pass structure to it by reference
	
   return 0;
}

void display(Student *studentInfo)
{
   cout << studentInfo[0].name;
}
Last edited on
No need to post this same thread on multiple forums.
http://cplusplus.com/forum/general/86526/
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.
Last edited on
thanks for the input :)
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. :)
Last edited on
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.
I modified the program a little. but now when i come back from the getInput function to the main function the structure is still unchanged. what am i doing wrong? this is so confusing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct Student
{
	string id;
	string name;
	double grades[MAX_GRADES];
	double average;
};

void getFileInput(Student *);

int main()
{
	Student *studentInfo;	// Declaration for dynamically allocated array of structs
	
	getInput(studentInfo);	// Call getInput function and pass structure to it by reference
        cout << studentInfo[0].id;
  	return 0;
}

/*
    Get input function
*/

void getInput(Student *stu)
{
       stu = new Student[maxStu];	// Dynamically allocate an array of structs
      stu[0].id= 12345;
}
Last edited on
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

OR

2) Pass in a reference to stu, i.e. Student &stu

OR

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?
Last edited on
i cant allocate memory for it in the main function because i dont know the size of it until i get to thegetInput function
Well, you've not shown how maxStu is defined and assigned a value, so it's hard for us to tell that.

So that leaves options 1 and 2.
Last edited on
is 1 or 2 suppose to be in the header of the getInput function? because it i tried it and it gives an error
The prototype for your function (which may be in a header - I don't know, because you haven't posted it) and the method definition itself should both correctly specify which types you're passing in.
void getFileInput(Student &); // prototype

getFileInput(studentInfo); // mehod call

void getFileInput(Student &stu) // method header

gives error on mehod call

error: invalid initialization of reference of type 'Student&' from expression of type 'Student*'
exercise2.cpp:16:5: error: in passing argument 1 of 'int getFileInput(Student&)'

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:

http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer

I gave it a quick read and it seems to explain the syntax of doing this quite nicely (although ignore the small section on CLI if it's not relevant to you).
Last edited on
thank you
Topic archived. No new replies allowed.