In the proof of concept code below, I pass a button structure to a function. Inside the function a strcut value is changed but the change only occurs locally. The attempt to set a new struct value is in the 3rd line from the bottom.
1) I thought struct definitions were passed by reference not value?
2) What do I need to do to change the struc values in a function?
1) I thought struct definitions were passed by reference not value?
2) What do I need to do to change the struc values in a function?
C:
/***************************************************************************
* button proof of concept *
***************************************************************************/
#include "allheaders.h"
// #include <stdio.h>
struct button {
int *btPtr;
int x;
int y;
} btExit;
void btExitFn ( struct button);
int main ( ) {
void ( *btExitPtr ) ( struct button ) = btExitFn; // assign ptr to btExitFn addr
btExit.btPtr = btExitPtr; // bt func address
btExit.x = 10; // init struct values
btExit.y = 20;
btExitFn ( btExit ); // call btExit before x is 50
btExitFn ( btExit ); // call btExit after x is 50
printf ("btExit addr : %lu\n", btExitFn); // fn addr
printf ("btExitPtr : %lu\n", btExitPtr); // ptr fn addr
printf ("btExit.btPtr : %lu\n", btExit.btPtr); // stored fn addr
return 0;
}
void btExitFn ( struct button btExit ) {
printf ( "x = %d\n", btExit.x );
btExit.x = 50; // <-- why not globally changed
printf ( "x = %d\n\n", btExit.x );
}