3

I have this code made for C++ (it works):

char* ConcatCharToCharArray(char *Str, char Chr)
{
    char *StrResult = new char[strlen(Str) + 2]; 
    strcpy(StrResult, Str);
    StrResult[strlen(Str)] = Chr;
    StrResult[strlen(Str) + 1] = '\0';
    return StrResult;
}

/* Example: String = "Hello worl"
Char = "d"
Final string = "Hello world" */

The little problem is that I'm making a standard C program in Ubuntu and I need this code. And "new" is NOT being recognized as a reserved word and there's even a red mark under it.

I tried: char *StrResult[strlen(Str) + 2], but it doesn't work because that way only admits constant values. I'm guessing "malloc" would be the standard C solution in here, how could I do this with "malloc" or any other way for that matter? Thank you so much.

soulblazer
  • 1,178
  • 7
  • 20
  • 30

3 Answers3

5

new is the C++ way of allocating memory. In C you're right, you need to use malloc.

char* ConcatCharToCharArray(char *Str, char Chr)
{
    size_t len = strlen( Str );
    char *StrResult = malloc( len + 2 );
    /* Check for StrResult==NULL here */
    strcpy(StrResult, Str);
    StrResult[len] = Chr;
    StrResult[len+1] = '\0';
    return StrResult;
}

When you're done with the memory, you'd call free( StrResult ).

Graeme Perrow
  • 56,086
  • 21
  • 82
  • 121
  • Thank you so much and everyone for your helpful answers. We were taught C++ and this new professor started telling us to program everything in C and Ubuntu. The only thing he managed was confuse the entire class between what belongs to C and what belongs to C++. Thank you all again. – soulblazer Jun 17 '14 at 01:45
  • @Graeme Perrow can you please explain why the `malloc(len + 2)` ? I meant why +2? Why it couldn't +1? – inckka Mar 15 '17 at 07:19
  • All strings in C must end with a null byte (`'\0'`). When allocating memory for a string, you must allocate enough bytes for the characters in the string plus one for the null byte. In this case, you need to allocate enough bytes for the characters already in the string (len) plus 1 for the extra character you're adding to the string, and then another +1 for the null byte. – Graeme Perrow Mar 15 '17 at 12:29
1

Yes, you need malloc and you are confusing C with C++ here (since new comes from C++):

char *StrResult = (*char) malloc((strlen(Str) + 2) * sizeof(char));

char takes only one byte (see this question), so you don't need to multiply by it's size:

char *StrResult = (*char) malloc(strlen(Str) + 2);
Community
  • 1
  • 1
DiPi
  • 99
  • 1
  • 7
  • 3
    [Don't cast the return value of malloc in C](http://c-faq.com/malloc/mallocnocast.html) –  Jun 17 '14 at 03:31
0

One way:

char* ConcatCharToCharArray(char *Str, char Chr)
   {
   size_t StrLen = strlen(Str);
   char *StrResult = malloc(StrLen + 2); 
   if(NULL == StrResult)
      goto CLEANUP;

   strcpy(StrResult, Str);
   StrResult[StrLen++] = Chr;
   StrResult[StrLen] = '\0';

CLEANUP:
   return StrResult;
   }

However, the above allocates a new string, instead of concatenating a character to an existing string. Here is a way to expand an existing string with an additional character:

int StrConcatChar(char **string, char character)
   {
   int rCode=0;
   size_t stringLen;
   char *temp;

   if(NULL == string)
      {
      rCode=EINVAL;
      goto CLEANUP;
      }

    stringLen = *string ? strlen(*string) : 0;

    errno=0;
    temp=realloc(*string, stringLen+2);
    if(NULL == temp)
       {
       rCode=errno?errno:ENOMEM;
       goto CLEANUP;
       }

    *string=temp;
     (*string)[stringLen++] = character;
     (*string)[stringLen] = '\0';

CLEANUP:

   return(rCode);
   } 

The above function might be called like this:

{
int rCode=0;
char *buffer=NULL;

buffer=strdup("Hello worl");
if(NULL == buffer)
   /* handle error condition */

rCode=StrConcatChar(&buffer, 'd');
if(rCode)
   /* handle error condition */

...

if(buffer)
   free(buffer);
}
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28