-1

What is wrong with the function. How come nothing gets printed out?

char * CombineStr(char * str1, char * str2)
{
    char strOut[256];
    sprintf(strOut, “%s%s”, str1, str2);
    return strOut;
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Tai Wu
  • 83
  • 1
  • 3
  • 6

3 Answers3

3

You should take care of scope (storage duration of your variable): either declare returned variable as static, or allocate it dynamically on the heap, using malloc, and freeing the allocated memory pointed to by strOut, once not needed anymore. You should use plain quotes in the format of your sprintf second argument. You should take care against overflow.
either:

char * CombineStr(char * str1, char * str2)
{
    static char strOut[256];                 //scope!
    if((strlen(str1) + strlen(str2)) < 256)
        sprintf(strOut, "%s%s", str1, str2); //plain quotes!
    return strOut;
}

or:

char * CombineStr(char * str1, char * str2)
{
    char strOut = malloc(strlen(str1) + strlen(str2) + 1);               
    if( strOut != NULL )
        sprintf(strOut, "%s%s", str1, str2); //plain quotes!
    return strOut;
}

For further reading, please take a look at following SO posts: 1, 2.

Community
  • 1
  • 1
user3078414
  • 1,942
  • 2
  • 16
  • 24
  • 2
    Hack with `static` won't work well if user decides to call `CombineStr()` few times and use cached results later. And what about multithreading environment? – Sergio Jul 30 '16 at 20:22
  • Thanks @Serhio. Added example with dynamic allocation as well. – user3078414 Jul 30 '16 at 20:27
2

Another common solution is to have the caller provide the output buffer

char * CombineStr(const char * str1, 
                  const char * str2, 
                  char * strOut, 
                  size_t outlen)
{
    size_t len = snprintf(strOut, outlen, "%s%s", str1, str2);
    if (len < outlen)
    {
        return strOut;
    }
    return NULL;
}

Note the switch from sprintf to snprintf to prevent buffer overflow should the concatenated string exceed the length of the buffer. This allows us to catch the overflow and return an invalid result to let the caller know buffer cannot be trusted.

Typical usage would be

char buffer[256];
if (CombineStr("I's the b'y that builds the boat", 
               "And I's the b'y that sails her", 
               buffer,
               sizeof(buffer)) != NULL)
{
    // use buffer
}

It needs to be noted that snprintf has wonky support and cannot always be trusted to have been null terminated, but you can be sure that it didn't overflow the buffer.

user4581301
  • 33,082
  • 7
  • 33
  • 54
0

Change to static char strOut[256];. It is not very good solution but will work.

i486
  • 6,491
  • 4
  • 24
  • 41
  • 1
    Drawbacks of such approach should be described IMO. – Sergio Jul 30 '16 at 20:17
  • @Serhio The drawback is that static buffer is only one and if someone is using `CombineStr` for example in `sprintf` as 2 or more parameters (2+ calls), the result will be wrong. For that reason my solution is simple and working but not for all cases. – i486 Jul 30 '16 at 23:07