Shifting array elements returns unexpected value

164 views Asked by At

I am programming for a STM32 ARM based board and I get the following problem:

I am moving elements inside an array but I get unexpected values that are not part of the array. Here is my code:

int temp=*manual_items[selec].Byte_Control[2];
*manual_items[selec].Byte_Control[2]=*manual_items[selec].Byte_Control[1];
*manual_items[selec].Byte_Control[1]=*manual_items[selec].Byte_Control[0];
*manual_items[selec].Byte_Control[0]=temp;

The values inside the array are (0,0,1) for example, so I expect to get (0,1,0) but, instead I get (224,1,0). This makes me suspect that some kind of garbage is being stored in temp, but I have tried initializing it to 0 when declared, getting the same result.

Also, using the same approach in a different part of the code works:

int temp=*manual_items[selec].Byte_Control[3];
*manual_items[selec].Byte_Control[3]=*manual_items[selec].Byte_Control[2];
*manual_items[selec].Byte_Control[2]=*manual_items[selec].Byte_Control[1];
*manual_items[selec].Byte_Control[1]=*manual_items[selec].Byte_Control[0];
*manual_items[selec].Byte_Control[0]=temp;

This shifts the values perfectly, so I can't understand why the first is not working and the second yes. Which could be the problem?

If it helps, I'm using Keil uVision 5 IDE.

PS: I know there are better ways to shift values in arrays, this is just a test I am doing.

EDIT: manual_items[] is an array of MANUAL_t, which is a struct I created. This is the struct:

typedef struct _manual_
{
  char Name[20];
  uint8_t *Byte_Control[4];
  STATUS_t *Status;
  status_type type;
  int Color_Ref;
  char Display_Ref[10];
} MANUAL_t;

So byte_control[] is of type uint8_t.

EDIT 2: This is the declartion of manual_items:

MANUAL_t manual_items[]=
{
  {"Barrier",{&BARRIERMAN},&status_items[3],UPDOWN,0,0},
  {"Sem. 1",{&SEM1GREENMAN,&SEM1YELLOWMAN,&SEM1REDMAN},&status_items[8],SEMAPHORE,0,0},
  {"Sem. 2",{&SEM2GREENMAN,&SEM2YELLOWMAN,&SEM2REDMAN},&status_items[9],SEMAPHORE,0,0}
};

EDIT 3: It finally works!! In the end the problem was not related to this piece of code, which is why I could not find what was causing the error. I had forgotten to put break;after two switch cases, which made the program go crazy. Thanks to all for your help and remember that mistakes are where you would expect the least!

0

There are 0 answers