Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing to a function just a member of a struct

Status
Not open for further replies.

sirbu

Programmer
Sep 15, 1999
15
0
0
RO
Can it be passed to a function just a member of a struct so that the function changes that member?

Let's say a have a struct

struct node {
double attribute1, attribute2;
} *n;

I define an array of the above tipe,

n = (struct node *) calloc (666,(sizeof (struct node)));


The question is if there is some way to pass to a function just let's say the attribute1 member so it will be assigned with some computed value ? If the answer is affirmative please show me how.

Thanks in advance.

 
Your question seems not very clear.

if you only need a memeber of the node to be assigned in a function, just name the function as
void GetVal(double &aVal){
aVal = ...
}

you can call it as :

GetVal(n->attribute1);
 
well... this is a round about way to do it but

void setStruct(const node* setTo, node* setThis, size_t size)
{
for(int i = 0;i<size;i++)
{
memcpy((setThis+i),setTo,sizeof(node));
}
}

and the call would look like this

node baseValue;
baseValue.attribute1 = 5.0;
baseValue.attribute2 = 7.0;

setStruct(&baseValue,n,666);

Matt
 
I believe I may have mis-understood your question. It seems you didnt mean initialization. If you just want to pass &quot;attribute1&quot; to a function you could pass it as jfhuang said above
 
My question is allmost what jfhuang answered. GetVal(n->attribute1); works fine for a scalar but how to do for a vector, because something like GetValue(n(i)->attribute1) won't work (sorry for the round brackets!).
So i think what Zyrenyhian answerd is closer to my question.
Thanks both
 
oh... what you want to do is

(n+i)->attribute1 or *n.attribute1

matt
 
ummm... thats
Code:
n[i].attribute1
bit of a typo above.

Matt
 
Guys,
think at solving a linear system of equations.
You can use a function like
void Solver(double A[], double B[], double *X)
where you pass two vectors A and B and the result is returned in X vector.
I want to pass to the function Solver my vector of structures (but not all, just the attribute1 member) instead of vector X so that the result is returned in attribute1 of each component of the vector of structures.
Maybe now it's clear what i want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top