Can't assign values to array I just made.

I can't seem to access the arrays I have made: 'buff' and 'Time'. I am even unable to initialize them. I am watching the locals and the variables 'i' and 'TimeVar' show changes, but nothing seems to affect the arrays.

Problem:

  // Variables
  //-- THE FOLLOWING WORKS JUST FINE --//
  s8  i       = 0;
  s32 TimeVar = 0;

  //-- THE FOLLOWING DOES NOT INITIALIZE THE ARRAYS --//
  s8  buff[4] = { 0, 0, 0, 0 };
  s8  Time[6] = { -1, -1, -1, -1, -1, -1 };   // { ss, mm, hh, dd, MM, yy }

  // I know I'm doing something a little weird here, but this is not causing my
  // my problem, I added this trying to figure out why it is not working.
  for (buff[0] = 4; buff[0] > 0; buff[0]--) // Clear the buffer
  {
    //-- HANGS HERE --//
    buff[buff[0] - 1] = 0;
  }

Complete function:

/*******************************************************************************
 * Function Name  : SetRTC
 * Description    : Sets the realtime clock baised on the 12 byte command input
 * Input          : "ssmmhhddmmyy"
 *                  "ss" - Seconds  ["00" to "59"]
 *                  "mm" - Minutes  ["00" to "59"]
 *                  "hh" - Hours    ["00" to "23"]
 *                  "dd" - Day      ["01" to "31"]
 *                  "MM" - Month    ["01" to "12"]
 *                  "yy" - Year     ["09" to "99"]
 * Output         : Displays an error message if it parses unexpected values
 * Return         : 0 - Error, RTC not set
 *                  1 - Success, RTC set
 *
 * Notes          : The time is represented as the number of seconds after
 *                    00:00:00 Jan 1, 2000
 *
 *  Example       : "M361014251011"
 *                  Time - 14:10:36 (2:10:36 pm)
 *                  Day  - 25 October 2011
 *                  RTC Value - 372,867,036 == 1639 7FDC
 ******************************************************************************/
int SetRTC(char *cmd)
{
  // TODO: Should these be #define?
  const short RTC_MIN[6] = {  0,  0,  0,  1,  1,  9 };
  const short RTC_MAX[6] = { 59, 59, 23, 31, 12, 99 };
  const short MONTH_TO_DAY[12] = { 31, 28, 31, 30, 31, 30,
                                   31, 31, 30, 31, 30, 31 };

  // Variables
  //-- THE FOLLOWING WORKS JUST FINE --//
  s8  i       = 0;
  s32 TimeVar = 0;

  //-- THE FOLLOWING DOES NOT INITIALIZE THE ARRAYS --//
  s8  buff[4] = { 0, 0, 0, 0 };
  s8  Time[6] = { -1, -1, -1, -1, -1, -1 };   // { ss, mm, hh, dd, MM, yy }

  // I know I'm doing something a little weird here, but this is not causing my
  // my problem, I added this trying to figure out why it is not working.
  for (buff[0] = 4; buff[0] > 0; buff[0]--) // Clear the buffer
  {
    //-- HANGS HERE --//
    buff[buff[0] - 1] = 0;
  }

  // Parse command string
  for(i = 0; i < 6; i++)
  {
    memcpy((&buff[0]), cmd + (i * 2), 2);  // Copy two digits from command to buffer

    Time[i] = atoi(&buff[0]);         // Convert buffer to ineger

    if ((Time[i] < RTC_MIN[i]) || // Check min/max values
        (Time[i] > RTC_MAX[i]))
    {
      printf("\r\nbad RTC date"); // Display error message
      return No_Commands_Run;     // Return without setting RTC
    }
  }

  // Verify the specified month actually has the specified days
  if (MONTH_TO_DAY[Time[4]] >= Time[3])
  {
    if ((Time[4] == 2) && (Time[3] == 29) && (Time[5] % 4 == 0))
    {
      // February 29th of a leap year, not an error
    }
    else
    {
      printf("\r\nbad RTC date");   // Display error message
      return No_Commands_Run;       // Return without setting RTC
    }
  }

  // Calculate the easy ones:
        TimeVar = Time[0]             +  // Seconds
            Time[1]      * 60   +  // Minutes
            Time[2]      * 3600 +  // Hours
           (Time[3] - 1) * 86400;  // Days

  // Add days for earlier months in the year
  for (i = Time[4] - 2; i >= 0; i--)
    TimeVar += MONTH_TO_DAY[i];

  // Add years for previous years since 2000;
  TimeVar += Time[5] * 365 * 86400;

  // Add leap days
  if(Time[4] <= 2)  // If it is Jan or Feb, hold back a year (see next comments)
    TimeVar += ((Time[5] + 3) / 4) * 86500;   // 2000 = +0 leap days
  else
    TimeVar += ((Time[5] + 4) / 4) * 86500;   // 2000 = +1 leap day

  // Set the clock
        RTC_Configuration();     // Initialize RTC
        RTC_WaitForLastTask();   // Wait for command to complete
  RTC_SetCounter(TimeVar); // Set RTC
        RTC_WaitForLastTask();   // Wait for command to complete

  // TODO: Remove after testing
  ReadRTC();

  return Single_Command;
}

Parents
  • What happens when you have a for loop, where your "last" iteration have value 1 in the loop element.

    You then assign 1-1 = 0 to that element, and then perform a -- operation on the element.

    You finally perform new check if element is zero - do you think you will ever see your loop variable reach zero?

    memset(buf,0,sizeof(buf)) is a quite good method to zero a buffer. Why not use it.

    Or using a "real" loop variable. With a "normal" loop variable, the compiler can make use of processor registers, producing smaller code and optionally not have to allocate any memory for the loop variable. When abusing an element of the array as a loop variable, the compiler must produce much worse code.

    Never try "clever" code unless you are very skilled and know exactly what happens. "clever" code will almost always come back and haunt you.

    Working code always wins over "clever" code that is buggy.

Reply
  • What happens when you have a for loop, where your "last" iteration have value 1 in the loop element.

    You then assign 1-1 = 0 to that element, and then perform a -- operation on the element.

    You finally perform new check if element is zero - do you think you will ever see your loop variable reach zero?

    memset(buf,0,sizeof(buf)) is a quite good method to zero a buffer. Why not use it.

    Or using a "real" loop variable. With a "normal" loop variable, the compiler can make use of processor registers, producing smaller code and optionally not have to allocate any memory for the loop variable. When abusing an element of the array as a loop variable, the compiler must produce much worse code.

    Never try "clever" code unless you are very skilled and know exactly what happens. "clever" code will almost always come back and haunt you.

    Working code always wins over "clever" code that is buggy.

Children
More questions in this forum