Why I write code creator 2.0, there will be two different situations

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ShSh_291626
Level 3
Level 3

 Why I write code creator 2.0, there will be two different situations

   

I wrote

   

{

   

commTxBuffer [4 + dptr + dptr] = modbusRAM [commRxBuffer [3] + dptr ++];

   

}

   

And writing

   

{

   

commTxBuffer [4 + dptr + dptr] = modbusRAM [commRxBuffer [3] + dptr];

   

dptr ++;

   

}

   

Will get different results?

   

(Translation on google translate)

0 Likes
6 Replies
Anonymous
Not applicable

1. NEVER have a vaiable on the left side changed on the right side

   

your dptr is modified on the right side, so when saving the value, it would used the CHANGED dptr

   

In your second example, your dptr doesn't change on the first line, it was incremented in the 2nd line, after the write instruction

0 Likes
Anonymous
Not applicable

I also would not use dptr as dptr should be a reserved name in PSOC3 which is a 8051 core.

0 Likes
kemic_264446
Level 4
Level 4
First like received

 The basic principle here is that the expression on the right hand side of "=" is evaluated before the one on the left. So the pointer is incremented before being used in the assignment.

   

 

   

-Kenny

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The problem is that in C it is not defined what evaluation has to be done first, the address of the result (LValue) or the expression at the right hand side of the equal sign. So a change of / in the compiler could induce a completely different behaveour. A nightmare for every programmer!

   

Have a look here http://publications.gbdirect.co.uk/c_book/chapter8/sequence_points.html at "Sequence Points" and see, that exactly the problem you have got is mentioned: NEVER use constructs like

   

a{i] = i++;

   

 

   

Happy coding

   

Bob

0 Likes
ShSh_291626
Level 3
Level 3

 I use Designer writing

   

{

   

commTxBuffer [4 + dptr + dptr] = modbusRAM [commRxBuffer [3] + dptr ++];

   

}

   

Will be equal to Create 2.0

   

{

   

commTxBuffer [4 + dptr + dptr] = modbusRAM [commRxBuffer [3] + dptr];

   

dptr + +;

   

}

   

(Translation on google translate)

0 Likes
Anonymous
Not applicable

 It would be better to use the second method. 

   

Don't use the first one.

   

It is a general pratise for any compiler or IDE.

0 Likes