how to pass a structure members to a function
hi
how to pass a structure members to a function.. and then initialize those members of a structure.. show an example by taking structure members as pointers.
thanks in advance
# 2 Re: how to pass a structure members to a function
struct Struct {
int a;
int b;
};
void Init(Struct* s)
{
s->a = 1;
s->b = 2;
}
int main()
{
Struct ss;
Init(&ss);
}
[almost] equivalently (for programmer "only" difference is, that you can't pass NULL through reference):
struct Struct {
int a;
int b;
};
void Init(Struct& s)
{
s.a = 1;
s.b = 2;
}
int main()
{
Struct ss;
Init(ss);
return 0;
}
sorry, what do you mean exactly by "taking structure members as pointers"?
this? :
struct Struct {
int* a;
int* b;
};
then you'd use something like:
void Init(Struct& s)
{
s.a = new int;
*s.a = 0;
s.b = new int;
*s.b = 0;
}