Here is the layout of my current code,
test.h
typedef struct{
int aa;
int bb;
int cc;
} ABC;
extern ABC XYZ;
void passValue(ABC DEF,int a, int b, int c);
void doSomething();
test.c
ABC XYZ;
void passValue(ABC DEF,int a, int b, int c){
DEF.aa = a;
DEF.bb = b;
DEF.cc = c;
}
void doSomething(){
...
...
passValue(XYZ,10,20,30);
}
but when I call doSomething() on the main program, the value is still 0 (not changed). I've try to treat them just like @R Sahu answer on this question,
test.h
typedef struct{
int aa;
int bb;
int cc;
} ABC;
extern ABC XYZ;
void passValue(ABC * DEF,int a, int b, int c);
void doSomething();
test.c
ABC XYZ;
void passValue(ABC * DEF,int a, int b, int c){
*DEF.aa = a; //error
*DEF.bb = b; //error
*DEF.cc = c; //error
}
void doSomething(){
...
...
passValue(&XYZ,10,20,30);
}
ERROR : expression must have struct or union type
Unary operators have less priority then postfix operators. So an expression like this
*DEF.aa = a; //error
is equivalent to
*( DEF.aa ) = a;
So you need at first apply the unary operator * and only then the postfix operator
( *DEF ).aa = a;
( *DEF ).bb = b;
( *DEF ).cc = c;
Or you could use only the postfix operator ->
DEF->aa = a;
DEF->bb = b;
DEF->cc = c;
As for the first function definition then function parameters are its local variables. Parameters are initialized by copies of the values of arguments. So within the function it is the copies that are changed. The original arguments are unchanged. You need to pass arguments by reference if you are going to change them in the function that is by using pointers to them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With