This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Keil and array function.

Hi guys,

I'm using Keil V5.8 and I'm trying to write a C function that converts an integer to a string which is then passed onto another function that displays it on an LCD. The code I'm using is

unsigned int lengthOfDigits = 20;
int IntergerToBeDisplayed = 106;
char StringNumber [lengthOfDigits];
sprintf(StringNumber, "%d", IntergerToBeDisplayed);

LCDSendAString(StringNumber);

Using the above code does not work at all, it builds without any errors or warnings but the display does not respond.  But using a constant value for the array size instead of passing a value works. char StringNumber[20];

From what I have researched, before C99 using an array in such a way was not possible in C. I have made sure that Keil is configured for C99 and I have also tested a version of the code in code blocks ide which works fine

#include <stdio.h>
#include <stdlib.h>

int main()
{
unsigned int lengthOfDigits = 20;
int numberOfElements = 120;
char digits [lengthOfDigits];
sprintf(digits, "%d", numberOfElements);
printf("%s", digits);
return 0;
}

Am I missing something here? I'm still new at embedded programming so if my question does not make sense please let me know and I will change it. 

Parents
  • Perhaps disconnect the issue from the display? Subdivide the problem. What it does on a PC is immaterial.

    Check that the data in your digits buffer actually looks correct in the debugger.

    Test the display directly LCDSendAString("Hello");

    Test it also with your string sprintf(StringNumber, "INT %d", IntegerToBeDisplayed);

    Consider using itoa() function, it is less resource heavy.

Reply
  • Perhaps disconnect the issue from the display? Subdivide the problem. What it does on a PC is immaterial.

    Check that the data in your digits buffer actually looks correct in the debugger.

    Test the display directly LCDSendAString("Hello");

    Test it also with your string sprintf(StringNumber, "INT %d", IntegerToBeDisplayed);

    Consider using itoa() function, it is less resource heavy.

Children