passing data structures into a function

J

John Phung

How do you pass a data structure into a function? I'm using c++ to solve the
producer and consumer problem and so far, I cannot find an example of how to do
this. So, if anyone can help me, that would be nice.
 
J

John Harrison

John Phung said:
How do you pass a data structure into a function? I'm using c++ to solve the
producer and consumer problem and so far, I cannot find an example of how to do
this. So, if anyone can help me, that would be nice.

It's not difficult, you can pass a data structure into a function using
exactly the same methods as you would pass a simple type.

DataStructure my_data_structure;
some_function(my_data_structure);

some_function could be declared like this

void some_function(DataStructure a_data_structure);

like this

void some_function(DataStructure& a_data_structure);

or like this

void some_function(const DataStructure& a_data_structure);

depending on exactly what you are trying to do.

But really your options are exactly the same as if you are trying to pass an
int into a function.

john
 
M

Mike Wahler

John Phung said:
How do you pass a data structure into a function? I'm using c++ to solve the
producer and consumer problem and so far, I cannot find an example of how to do
this. So, if anyone can help me, that would be nice.


#include <iostream>

// define a structure
struct X
{
int m;
};

// a function with a structure as a parameter
void function(X arg)
{
std::cout << arg.m << '\n'; // prints 42
}

int main()
{
X obj = {42}; // create an instance of type 'X'
function(obj); // pass 'X' to 'function()'
return 0;
}


If the structure is 'large', it's usually better to
pass by reference:

void function(X& arg)
{
// etc
}

And if the function doesn't modify the argument, pass
by const reference:

void function(const X& arg)
{
// etc
}

In all cases above, the calling syntax is the same.

Another way is by using a pointer parameter, which
does change the calling syntax:

void function (X *arg) // or const X *arg if applicable
{
// etc
}

call with:

X obj = {42};
function(&obj);

Which C++ book(s) are you reading?

-Mike
 
K

Karl Heinz Buchegger

John said:
How do you pass a data structure into a function? I'm using c++ to solve the
producer and consumer problem and so far, I cannot find an example of how to do
this.

????
What books are you using?
So, if anyone can help me, that would be nice.

struct test
{
int a;
int b;
}

void foo( test Arg )
{
std::cout << Arg.a;
std::cout << Arg.b;
}

int main()
{
test MyVar;

MyVar.a = 4;
MyVar.b = 8;

foo( MyVar );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top