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

6
  • 2
    Possible duplicate of concatenate char array in C Commented Apr 18, 2017 at 4:49
  • 1
    added an explanation how mine is differ from previous question Commented Apr 18, 2017 at 5:03
  • Welcome to Stack Overflow! Please show your research/debugging effort so far. Please read How to Ask page first. Commented Apr 18, 2017 at 5:05
  • See also Does this function I made correctly append a string to another string? Commented Apr 18, 2017 at 5:32
  • Create a temporary string then copy it. strcat(tempPrefix, (char[2]){ root->label, '\0'}); Commented Apr 18, 2017 at 8:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am not getting expected output here. I can see appended char is in 28th location of my tempPrefix char array but when I print it, it is not there. Shouldn't we concat to the next location of current text in array rather than to the end of array.
@GeorgeKlimas - Minor mistake on my part. strncpy copies at most count characters, inclusive. That means the call to it needs to be adjusted. See my edit for the detail.
Still I am not getting appended character in the char array. Debug shows me that character is being appended to 28 position.
@GeorgeKlimas - Yes, I was editing that fix into the question, since I misunderstood the parameters of your problem initially.
@GeorgeKlimas - NP. Glad to help.
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
}

1 Comment

I am not getting the output here. I can see res is getting minus values. Is it the problem ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.