Solved

strncat function

Posted on 2004-09-06
7
332 Views
Last Modified: 2008-03-04
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?
0
Comment
Question by:ee_guest
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 15

Accepted Solution

by:
efn earned 100 total points
ID: 11993083
I don't think the problem is in your strncat function.  The problem is that your test case shouldn't work, and if it worked with the library strncat, that was just luck.

As you said, dest is an array of 5 characters, so when you append 10 characters to it, the data is likely to clobber something else, and you get undefined behavior.

So I suggest you make the dest array big enough to hold what you want to store in it.  Then I think you will find that your strncat function works.

You might want to make the num parameter unsigned, or else check it for a negative value.  The standard type is size_t, which is unsigned.

--efn
0
 
LVL 3

Expert Comment

by:realrael
ID: 11993561

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

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 11994403
Not sure if you can do this but can you malloc(sizeof(dest) + sizeof(src)) in a new char array, then printf from there?
0
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 11994405
similiar to what realrael just said, but dynamic.  Sorry, 3:30AM, time for bed.
0
 
LVL 22

Expert Comment

by:grg99
ID: 11995633
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.


0
 
LVL 15

Expert Comment

by:efn
ID: 11998671
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
0
 
LVL 22

Expert Comment

by:grg99
ID: 11998710
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

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now