Skip to main content
RZub
Associate II
March 8, 2022
Solved

Issue using pointers

  • March 8, 2022
  • 4 replies
  • 2199 views

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.

This topic has been closed for replies.
Best answer by KnarfB

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

4 replies

KnarfB
KnarfBBest answer
Super User
March 8, 2022

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

S.Ma
Principal
March 8, 2022

Some compilers optimise too much.

Try to declare workModeObj as volatile temporarily.

KnarfB
Super User
March 8, 2022

"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

RZub
RZubAuthor
Associate II
March 8, 2022

Thank You for your help. The issue is solved.

Andrew Neil
Super User
March 8, 2022

@Community member​  Please verify the answer which solved your issue:

0693W000008y9fZQAQ.png

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Piranha
Principal III
March 17, 2022

And now an answer, which is wrong and doesn't solve the problem, is selected as the best... :D

TDK
Super User
March 8, 2022

> *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.

"If you feel a post has answered your question, please click ""Accept as Solution""."