Dear C experts,
First and foremost, please let me thank you for all your help in this forum. You are making my coding life much, much more pleasant :-)
Now, to the point. I need to create a struct in which member points to another (global) variable. As I am to pass this struct to functions, and pointers to pointers make me nervous, I thought of checking with the experts first.
Let me explain myself with a silly example. I know that if I define a function like this:
and then I call it asCode:void myfunc(double *varvalue) { *varvalue = somevalue; }
then the global variable "othervariable" will get the value "somevalue". (I hope I am correct here).Code:myfunc(&othervariable);
Well, now I need to do that with structs. My guess (which could be totally wrong) is that I should do the following:
Then, I would initialize it like this?Code:typedef struct _sType { double *value } sType; sType mystruct;
from main:
from a function:Code:*mystruct.value = &othervariable
Code:void initfunc(sType *s) { *s->value = &othervariable }
Now, to update the value of "othervariable" should I do this?
from main:
and from within functions:Code:*mystruct.value = somevalue;
Please let me now whether any (or all) of my assumptions are wrong.Code:void myfunc(sType *s) { *s->value = somevalue }
Thanks so much, as usual, for your help
mc61



LinkBack URL
About LinkBacks






