Issue using pointers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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.
- Labels:
-
STM32CubeIDE
-
STM32G0 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-08 2:05 AM
Some compilers optimise too much.
Try to declare workModeObj as volatile temporarily.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-08 3:04 AM
Thank You for your help. The issue is solved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-08 3: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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-08 4: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-08 8:36 AM
@Community member​ Please verify the answer which solved your issue:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-17 2:38 PM
And now an answer, which is wrong and doesn't solve the problem, is selected as the best... :D
