cancel
Showing results for 
Search instead for 
Did you mean: 

Issue using pointers

RZub
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

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

View solution in original post

7 REPLIES 7
KnarfB
Principal III

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

Some compilers optimise too much.

Try to declare workModeObj as volatile temporarily.

RZub
Associate II

Thank You for your help. The issue is solved.

KnarfB
Principal III

"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

TDK
Guru

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

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

0693W000008y9fZQAQ.png

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