Link to home
Start Free TrialLog in
Avatar of ee_guest
ee_guest

asked on

strncat function

I wrote the below function

...
char dest[5] = "The ";
char src[10]="aeroplane";
...
printf("%s\n", strncat(dest, src, 9)); /* problem */
...
/*
concatenate num number of characters from src to dest
If num >= strlen(src), the whole string will be appended
*/
char *strncat(char *dest, const char *src, int num)
{
      /* save start of dest to return later */
      char *ptr = dest;

      /* point dest to null terminator in dest */
      dest = (char *)(dest + strlen(dest));

      /* copy from src string to dest string until num==0 or src==null */
      while ( num && *src )
      {
            *dest = *src;  /* copy value */
            dest++;               /* move to next */
            src++;               /* move to next */
            num--;
      }

      *dest = '\0';      /* set to NULL for last character */
      dest = ptr;            /* move pointer back to start */

      return ptr;
}


The strncat in string,h will work for above but not my strncat. The reason is dest is declared as char dest[5] and cannot hold more than 5 number of characters.

Can someone suggest me how to solve this?
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of realrael
realrael


you could make ptr a static variable of fixed length, say:

static char ptr[1001]; /* maximum of 1000 characters for the joined strings */

your code would be like:
1. copy dest to ptr. result: ptr = dest
2. add src to ptr. result: ptr = dest + src
3. return ptr.

notes:
1. dest and src would be unchanged.
2. if you need to save the concatenated strings to some variable, you have to take the responsibility that it doesn't mess up your memory. so:

a = strncat(b, c)

hope this helps.

- rael

Avatar of Kyle Abrahams, PMP
Not sure if you can do this but can you malloc(sizeof(dest) + sizeof(src)) in a new char array, then printf from there?
similiar to what realrael just said, but dynamic.  Sorry, 3:30AM, time for bed.
You shouldnt be calling strncat with that "5" !!!

The whole point of strncat is to not overflow the destination array.  You should always use "sizeof dest" as the third parameter.

Another problem, your strncat is incorrect,, in at least three ways:

(1) .  It will store up to "num" characters past the current end of "dest".  That is wrong, the definition is to limit the TOTAL length of dest to "num" characters.

(2)  It first stores a character past the end of dest, and THEN checks to see if there is any room left.

(3) It always stores a '\0' past "num" characters, even if there isnt room for it.


grg99, you must be thinking of some other function.

The third parameter to strncat limits the number of characters copied, not the total length of the resulting string.

So there is no concept of "room left," and if num and *src are both non-zero, it is correct to move a character.

It is also correct to store a null character after moving num characters.

--efn
Egads, you're right, I was thinking of strncpy(). !!  Apologies.

Still it would be nice to have a "safe" version of strcat that doesnt overlow its destination....

Regards,

grg99