2022-03-07 11:44 PM
Hello,
can someone help to identify where is the issue using pointers? The code example is below.
On line 36: workMode->current = workMode->intend;
It's not overwriting/changing the value.
If I change it to like this:
*workMode->current = *workMode->intend;
Then, I'm getting an error:
error: invalid type argument of unary '*' (have 'int')
Code example:
/* Enum */
typedef enum
{
enWorkMode_0 = 0,
enWorkMode_1 = 1,
enWorkMode_2 = 2,
enWorkMode_3 = 3,
} WorkingModes_e;
/* Structure */
typedef struct
{
WorkingModes_e current;
WorkingModes_e intend;
} WorkModeObj_t;
/* Variable */
WorkModeObj_t workModeObj;
/* Declare */
void IndicationCheck(WorkModeObj_t *workMode);
/* Main */
int main(void)
{
workModeObj.current = enWorkMode_0;
workModeObj.intend = enWorkMode_1;
IndicationCheck(&workModeObj);
}
/* Function */
void IndicationCheck(WorkModeObj_t *workMode)
{
workMode->current = workMode->intend;
}
Using an online C compiler, it works. The link is here.
https://onlinegdb.com/LmREICaYl
Working with STM32CubeIDE v1.7.0 and MCU: STM32G0 series.
Solved! Go to Solution.
2022-03-08 12:39 AM
What is the gcc version and command line flags?
In you above code (without printf), the result is not used after assignment, so the compiler might optimize the assignment away. Feed your code to the compiler explorer https://godbolt.org/
hth
KnarfB
2022-03-08 12:39 AM
What is the gcc version and command line flags?
In you above code (without printf), the result is not used after assignment, so the compiler might optimize the assignment away. Feed your code to the compiler explorer https://godbolt.org/
hth
KnarfB
2022-03-08 02:05 AM
Some compilers optimise too much.
Try to declare workModeObj as volatile temporarily.
2022-03-08 03:04 AM
Thank You for your help. The issue is solved.
2022-03-08 03:14 AM
"too much" No, I like that. Days are gone when you had to make a bow in front of the compiler and write ugly code just to please Him. Optimization also forces us to make explicit what we are assuming on the code.
KnarfB
2022-03-08 04:15 AM
> *workMode->current = *workMode->intend;
You don't need the "*" here. The "->" gets the object from the pointer. You're trying to dereference it further, but you can't dereference an object of type WorkingModes_e.
2022-03-08 08:36 AM
@Community member Please verify the answer which solved your issue:
2022-03-17 02:38 PM
And now an answer, which is wrong and doesn't solve the problem, is selected as the best... :D