C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

passing data structures into a function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
John Phung
Guest





PostPosted: Thu Feb 26, 2004 10:54 pm    Post subject: passing data structures into a function Reply with quote



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.

Back to top
John Harrison
Guest





PostPosted: Thu Feb 26, 2004 10:58 pm    Post subject: Re: passing data structures into a function Reply with quote




"John Phung" <jphung (AT) rohan (DOT) sdsu.edu> wrote

Quote:
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



Back to top
Mike Wahler
Guest





PostPosted: Thu Feb 26, 2004 11:01 pm    Post subject: Re: passing data structures into a function Reply with quote



"John Phung" <jphung (AT) rohan (DOT) sdsu.edu> wrote

Quote:
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



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Thu Feb 26, 2004 11:08 pm    Post subject: Re: passing data structures into a function Reply with quote

John Phung wrote:
Quote:

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?

Quote:
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 );
}



--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.