Home > Software engineering >  define value couldnt change in function
define value couldnt change in function

Time:03-17

I want to change TORQUE_HWORD and TORQUE_LWORD global defines when I called the function If I called. How can I do that ?

I am trying like this:

uint16_t mAddressArray[10];
 #define TORQUE_HWORD mAddressArray[5]
#define TORQUE_LWORD mAddressArray[6]
void changeValue()
{
 Write_EEPROM_Define(torque, TORQUE_HWORD, TORQUE_LWORD); // Current Limit
 printf("A: x B: x ", TORQUE_HWORD, TORQUE_LWORD); // I see this value 0000 0000 
}
void Write_EEPROM_Define( float value, uint16_t eepromHValue, uint16_t eepromLValue)
{
 eepromHValue = 0xAA;
 eepromLValue = 0XBB;
}

CodePudding user response:

The main problem here is that you pass the array items by value, so you don't change the original array. And this problem was caused by using misleading macros when accessing an array.

Other problems is that void changeValue() is obsolete style, it should be void changeValue (void). And the function Write_EEPROM_Define is badly named, I would expect a function like that to perform an actual write to an EEPROM.

All these problems can be fixed like this:

uint16_t mAddressArray[10];
#define TORQUE_HWORD_INDEX 5
#define TORQUE_LWORD_INDEX 6

void changeTorque (float value, uint16_t array[10], uint16_t hi_index, uint16_t lo_index)
{
  array[hi_index] = 0xAA;
  array[lo_index] = 0xBB;
}

void changeValue (void)
{
  changeTorque(torque, mAddressArray, TORQUE_HWORD_INDEX, TORQUE_LWORD_INDEX);
  printf("A: x B: x ", mAddressArray[TORQUE_HWORD_INDEX], mAddressArray[TORQUE_HWORD_INDEX]);
}

As for why you have this array as a global variable yet pass it around between functions in the same file, I have no idea. That's just strange program design, you should perhaps make it a local static variable in some function instead.

  • Related