How do I pass a structure by reference to a function ?

 
 
Posts: 17
Joined: 2007-02-25
Points: 0
User is offline
How do I pass a structure by reference to a function ?

Hi All,
I am new to C and C++.
I want to pass a structure to a function by reference. How can I do that ?
Thanks,
JavaCoder


Posts: 18
Joined: 2007-02-25
Points: 0
User is offline
How do I pass a structure by reference to a function ?

Hi,
You can pass a structure by reference by passing the address of the structure and in the called function accept the address of the structure in a variable which is of type pointer to the structure.


e.g.struct p
{
int i; char c;
};
void func(struct p*);
void main()
{
struct p myp;
myp.i=10; myp.c='a';
func(&myp);
}
void func(struct p *mp)
{
printf("%d %c",mp->i,mp->c);
}
HTH,
Mahendra