Passing an array of structs

I have this struct

struct student//Struct layout
{
char name[30];
char id[11];
float gpa;
int age;
char gender;
};

this is the syntax I have for the function I want to send the array to:

void firstmenu(student students[]);

student students[5];//Struct Array, this is where I make the array

firstmenu(students[]);

and that is where I try to call it. I get a compiler error of:

error C2059: syntax error : ']'

when I try to call the function. So I am assuming I have the right parameters when I make the function but I am missing something when I call it. I tried it without the [] doing [5] and it still gives me errors. I'm not sure what I am missing.

Thanks in advanced, hopefully I gave enough information.




Answer this question

Passing an array of structs

  • Ingo

    Ah.... you're asking in the C# forum. In C++, the syntax is likely to be different. I'll move your post to the correct forum. I believe you need to pass a student * and an index to say how many students are in there, or a reference to a std::vector, but perhaps someone else has a better idea. Are you using C++/CLI That could open options as well.



  • gwarnes

    I get errors with that

    error C2501: 'students' : missing storage-class or type specifiers
    error C2146: syntax error : missing ')' before identifier 'students'
    error C2146: syntax error : missing ';' before identifier 'students'
    error C2059: syntax error : ')'

    I apologise if this is a dumb question I am still pretty new to C++.

  • Timothy1170

    well, add a body to the method, but otherwise, yes.



  • yazoox

    You're saying I should do

    void firstmenu(student [] students);

  • Alexkobe

    Where I declare the function

  • Kruthi

    Oh, I'm sorry heh. Thank you for the move.

    I'm just using C++ I believe. I didn't know there was a difference between C# and C++ lol.

  • itisasif

    I think you want student [] students, not student students []



  • sajid1jama

    I'm not sure I understand the question, because I can't see a non-obvious answer. You declare it in the class you want to be passing your array to.



  • Passing an array of structs