Add 1 char to char[2]

how to add a single char to char array
char array[2]={};
how to add single char to it in next line array=array+thing; doesnt work :grinning: :smiley: :grinning:

You could use strcat or strncat.

char a[4] = "ab";
char b[2] = "c";
strcat(a, b);
Serial.println(a);

also extra: is [1] is for store 2 chars (can use 0)?

array[0] = 'a';
array[1] = '\0';
Serial.println(array);

it resets arduino

tftchar(key,colortext);
strcat(code, key);

A string is terminated by a zero, so for a string of length n, you need an array of size n + 1. In the example of post #2, the array a is of size 4 to contain the characters a, b, c and \0.

The code string is probably not large enough. It needs to hold both its original content and the content of key plus the zero terminating character.

Please show all code

consider code below i just posted to append chars to a char array using an index, "idx", which is reset after processing the array contents (this was done on a laptop)

#include <stdio.h>
#include <string.h>

const int CodeLen = 20;
char code [CodeLen];
int  idx = 0;

void
loop (void)
{
    char key = getchar ();

    if (key)  {
        code [idx++] = key;
        if (CodeLen == idx || '#' == key)  {
            code [idx-1] = '\0';          // overwrite '#' w/ NUL
            idx        = 0;
            if (!strcmp ("AC", code))
                printf (" AC found\n");
            else
                printf (" unknown\n");
        }
    }
}

int
main ()
{
    while (1)
        loop ();
    return 0;
}

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