0

I am new to C language. I need to concatenate char array and a char. In java we can use '+' operation but in C that is not allowed. Strcat and strcpy is also not working for me. How can I achieve this? My code is as follows

void myFunc(char prefix[], struct Tree *root) {
    char tempPrefix[30];
    strcpy(tempPrefix, prefix);
    char label = root->label;
    //I want to concat tempPrefix and label

My problem differs from concatenate char array in C as it concat char array with another but mine is a char array with a char

Community
  • 1
  • 1
  • 2
    Possible duplicate of [concatenate char array in C](http://stackoverflow.com/questions/2218290/concatenate-char-array-in-c) – Harsha W Apr 18 '17 at 04:49
  • 1
    added an explanation how mine is differ from previous question – George Klimas Apr 18 '17 at 05:03
  • Welcome to Stack Overflow! Please show your research/debugging effort so far. Please read [Ask] page first. – Sourav Ghosh Apr 18 '17 at 05:05
  • See also [**Does this function I made correctly append a string to another string?**](http://stackoverflow.com/questions/43422030/does-this-function-i-made-correctly-append-a-string-to-another-string/43422468#43422468) – David C. Rankin Apr 18 '17 at 05:32
  • Create a temporary string then copy it. `strcat(tempPrefix, (char[2]){ root->label, '\0'});` – Lundin Apr 18 '17 at 08:44
  • Alternatively, `size_t length = strlen(tempPrefix); tempPrefix[length] = root->label; tempPrefix[length+1] = '\0';` – Lundin Apr 18 '17 at 08:46

2 Answers2

1

Rather simple really. The main concern is that tempPrefix should have enough space for the prefix + original character. Since C strings must be null terminated, your function shouldn't copy more than 28 characters of the prefix. It's 30(the size of the buffer) - 1 (the root label character) -1 (the terminating null character). Fortunately the standard library has the strncpy:

size_t const buffer_size = sizeof tempPrefix; // Only because tempPrefix is declared an array of characters in scope. 
strncpy(tempPrefix, prefix, buffer_size - 3);
tempPrefix[buffer_size - 2] = root->label;
tempPrefix[buffer_size - 1] = '\0';

It's also worthwhile not to hard code the buffer size in the function calls, thus allowing you to increase its size with minimum changes.


If your buffer isn't an exact fit, some more legwork is needed. The approach is pretty much the same as before, but a call to strchr is required to complete the picture.

size_t const buffer_size = sizeof tempPrefix; // Only because tempPrefix is declared an array of characters in scope. 
strncpy(tempPrefix, prefix, buffer_size - 3);
tempPrefix[buffer_size - 2] = tempPrefix[buffer_size - 1] = '\0';
*strchr(tempPrefix, '\0') = root->label;

We again copy no more than 28 characters. But explicitly pad the end with NUL bytes. Now, since strncpy fills the buffer with NUL bytes up to count in case the string being copied is shorter, in effect everything after the copied prefix is now \0. This is why I deference the result of strchr right away, it is guaranteed to point at a valid character. The first free space to be exact.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
0

strXXX() family of functions mostly operate on strings (except the searching related ones), so you will not be able to use the library functions directly.

You can find out the position of the existing null-terminator, replace that with the char value you want to concatenate and add a null-terminator after that. However, you need to make sure you have got enough room left for the source to hold the concatenated string.

Something like this (not tested)

#define SIZ 30


//function
char tempPrefix[SIZ] = {0};     //initialize
strcpy(tempPrefix, prefix);    //copy the string
char label = root->label;      //take the char value

if (strlen(tempPrefix) < (SIZ -1))   //Check: Do we have room left?
{
    int res = strchr(tempPrefix, '\0');  // find the current null
    tempPrefix[res] = label;             //replace with the value
    tempPrefix[res + 1] = '\0';          //add a null to next index
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261