[c++] Passing structs to functions

I am having trouble understanding how to pass in a struct (by reference) to a function so that the struct's member functions can be populated. So far I have written:

bool data(struct *sampleData)
{

}

int main(int argc, char *argv[]) {

      struct sampleData {
    
        int N;
        int M;
        string sample_name;
        string speaker;
     };
         data(sampleData);

}

The error I get is:

C++ requires a type specifier for all declarations bool data(const &testStruct)

I have tried some examples explained here: Simple way to pass temporary struct by value in C++?

Hope someone can Help me.

This question is related to c++ struct

The answer is


bool data(sampleData *data)
{
}

You need to tell the method which type of struct you are using. In this case, sampleData.

Note: In this case, you will need to define the struct prior to the method for it to be recognized.

Example:

struct sampleData
{
   int N;
   int M;
   // ...
};

bool data(struct *sampleData)
{

}

int main(int argc, char *argv[]) {

      sampleData sd;
      data(&sd);

}

Note 2: I'm a C guy. There may be a more c++ish way to do this.


Passing structs to functions by reference: simply :)

#define maxn 1000

struct solotion
{
    int sol[maxn];
    int arry_h[maxn];
    int cat[maxn];
    int scor[maxn];

};

void inser(solotion &come){
    come.sol[0]=2;
}

void initial(solotion &come){
    for(int i=0;i<maxn;i++)
        come.sol[i]=0;
}

int main()
{
    solotion sol1;
    inser(sol1);
    solotion sol2;
    initial(sol2);
}

It is possible to construct a struct inside the function arguments:

function({ .variable = PUT_DATA_HERE });