Convert int to char and append to another char[]

Hi all!! Hope everyone is well.

I'm having a problem converting an int to a char and then appending that char to another char[] array using strcat(). I've spent sometime researching this and can't figure out where I am going wrong. Here's my code

char display_value[4];       // input value stored here for display
int keypad_value;             // contains the keypad button pressed value or -1 if no button pressed

keypad_value = keypad.contains(ts_xPos, ts_yPos);     // get the keypad value

char temp_val = keypad_value + '0';
const char ccTemp_val = temp_val;
strcat(display_value, ccTemp_val, 1);
   

No matter what I've tried, I keep getting an error message when compiling:

strcat(display_value, ccTemp_val, 1);
                     ^~~~~~~~~~
  error: invalid conversion from 'char' to 'const char*' [-fpermissive]

So how am I going wrong here? This is running inside a loop, so keypad_value and ccTemp will be ever changing as user inputs data.

Thanks for any advice,
Randy

ccTemp_val is defined as a single char character.

strncat expects to be passed a pointer to a char array.

char display_value[4];       // input value stored here for display
int keypad_value;             // contains the keypad button pressed value or -1 if no button pressed

char ccTemp_val[2] =  {0};

void setup()
{
  keypad_value = 9;

  ccTemp_val[0] = keypad_value + '0';

  strncat(display_value, ccTemp_val, 1);
}

void loop()
{

}

Are you fracking serious? That was the problem! It's because I didn't define the size of the char array. I see that now!

I need to define my data better than I do, since that's what caused this problem.

Thanks @red_car,
Randy

No, that’s not the reason, strcat expects a pointer to char, you give it a char. try

const char temp_val = keypad_value + '0';
strncat(display_value, &temp_val, 1);

strncat

Yes, thanks

char display_value[4];       // input value stored here for display
int keypad_value;             // contains the keypad button pressed value or -1 if no button pressed

keypad_value = keypad.contains(ts_xPos, ts_yPos);     // get the keypad value

char temp_val = keypad_value + '0';
const char ccTemp_val = temp_val;
strcat(display_value, ccTemp_val, 1);

I for one would like to see the whole sketch because there is a simpler way to build the input string given an input in a char variable from the keypad but without seeing the context it is difficult to provide specific advice

Ah, passing the address of temp_val.. For some reason, I really struggle with pointers in c and I don't know why. I need to work on that, thanks!

Thanks!
Randy

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.