Originally Posted by
jackson6612
Thank you, Elysia.
I'm so sorry. I still don't get it. Let me tell you what I think about it.
I have read that when you call a function with an array as an argument, the value of the array is passed by reference by default - you don't need to use "&".
I still can't recognize the problem with my own code.
Let's say when "( i = 2 )" the function is called:
Code:
display(student[2]);
"student[2]" is the third array of type Student. The function also expects an array of this type - of type Student and of single dimension.
Where do I go wrong? Please guide me on this. Thanks.
An array is a block of contiguous memory - meaning your most likely going to have more than one element in that array. Secondly, the name of an array is the address of the first element in that array.
In your case you have N elements. In the following code you give the address of the first element in your array.
Code:
cout << "details of student #" << (i+1) << ":\n";
display(student);
Fine.
In your definition here you tell the compiler to expect an array again.
Code:
void display(Student dummystud[])
Fine.
However in the ensuing cout statements you don't tell the compiler to actually go over all the elements in that array.
Code:
cout << "roll no.: " << dummystud[].rollno;
cout << "name: " << dummystud[].name;
cout << "age: " << dummystud[].age;
cout << "marks: " << dummystud[].marks;
cout << "sex: " << dummystud[].sex;
}
an array is a block of contiguous memory. your compiler knows this and is expecting you to tell it which element to display. So how would you make it display all the elements in your array?
that is what this error
Code:
error: expected primary-expression before ']' token
is telling you. It's not going to magically go over each element for you. You have to tell it to.
something needs to be between those brackets