Enhanced concatenating function I would like to submit my enhanced concatenating function for your review. Originally, its method signature is:

char *concat (const *char1, const *char2);


but after knowing that the calling code must supply all the arguments when it calls the function, I modified the method signature to:

char *enhconcat (const char *str1, int start_index1, int end_index1, const char *str2, int start_index2, int end_index2);


supplying the necessary positional arguments, so I think the code is more secured and adaptable to future changes.

Please suggest ways to improve this code and make it more secure.

/* enhconcat.c: enhanced concat - for more secured concatenating */

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

char *
enhconcat (const char *str1, int start_index1, int end_index1,
           const char *str2, int start_index2, int end_index2);

int main (void) {

     const char *part1 = "Cuius est solum, ";
     const char *part2 = "eius est usque ad coelum et ad inferos.";
     printf ("%s\n", enhconcat (part1, 0, strlen (part1), part2, 0, strlen (part2)));
     return (EXIT_SUCCESS);
}

char *
enhconcat (const char *str1, int start_index1, int end_index1, 
           const char *str2, int start_index2, int end_index2)
{
   int m = end_index1 - start_index1;
   int n = end_index2 - start_index2;
   char *concatstr;
   concatstr = malloc (m + n + 1);
   if (!concatstr) {
      printf ("Unable to allocate memory.\n");
      exit (EXIT_FAILURE);
   }
   char *chrptr;
   chrptr = concatstr; // store address of concatstr
   for (int i = start_index1; i &lt; m; i++) {
      *chrptr = str1[i];
      chrptr++;
   }
   for (int j = start_index2; j &lt; n; j++) {
      *chrptr = str2[j];
      chrptr++;
   }
   *chrptr = '\0';
   return concatstr;
} /* end of enhconcat() */
Chris, you have misunderstood the text quoted about the caller supplying all of the arguments. Your original prototype was okay. I imagine the text you read meant that if a function declares parameters a, b and c, then the caller must supply arguments (ie values of some sort) for all of those parameters. If there are 3 it must supply 3,, and of the correct types.

Given the prototype you imposed upon yourself, your implementation was not bad, but you should learn to use the standard library functions.  Your loops for copying characters are better implemented using memcpy.

Also when you declare a variable, if you can it is often better to assign a value to it immediately, not on the next line. In C, this doesn't really matter, but in C++ doing this (assigning, or constructing immediately) is more efficient.

Here is my version of your function with its original prototype.  Note that instead of exiting on malloc failure, I print a message and return NULL. The caller must handle the failure.  Note also that allocated memory must be freed.  In a simple program like this, it is not important, as it will be freed on exit from the program. But remember that you have to do it normally.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

static char *
enhconcat (const char *str1, const char *str2)
{
    size_t len1 = strlen(str1);
    size_t len2 = strlen(str2);

    char *concatstr = malloc(len1 + len2 + 1);
    if (!concatstr) {
        perror("malloc");
        return NULL;
    }
    memcpy(concatstr, str1, len1);
    memcpy(concatstr + len1, str2, len2);
    concatstr[len1+len2] = '\0';
    return concatstr;
}

int main (void) 
{
     const char *part1 = "Cuius est solum, ";
     const char *part2 = "eius est usque ad coelum et ad inferos.";

     char *cat = enhconcat(part1, part2);
     if (cat != NULL) {
         printf ("%s\n", cat);
         free(cat);
     }
     return EXIT_SUCCESS;
}