7

I am trying to use strncpy to only copy part of a string to another string in C.

Such as:

c[] = "How the heck do I do this";

Then copy "do this" to the other string, such that:

d[] = "do this"

Help is appreciated.

squiguy
  • 32,370
  • 6
  • 56
  • 63
Church
  • 115
  • 2
  • 3
  • 11

4 Answers4

12

Just pass the address of the first letter you want to copy: strcpy(dest, &c[13]).

If you want to end before the string ends, use strncpy or, better, memcpy (don't forget to 0-terminate the copied string).

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • So assuming 'd' in 'do' is in the 20th position strncpy(dest, &c[20]), is that correct? or should "dest" be replaced by array d? – Church Sep 20 '12 at 00:31
  • Alomost. `strncpy` needs a length, `strncpy(dest, &c[20], 8)`. Whether dest is a `char*` pointing to a sufficiently large `malloc`ed memory area or a sufficiently large `char[N]` doesn't matter. – Daniel Fischer Sep 20 '12 at 00:33
  • Ok that makes sense, strcpy it is, but how do I assign that to array d then? – Church Sep 20 '12 at 00:34
  • What do you mean with assigning that to array `d`? `strcpy(d, &c[20])` copies the part of `c` from position 20 to the 0-terminator (inclusive) into the array `d`. – Daniel Fischer Sep 20 '12 at 00:40
  • Yes, that was what I was looking for! I kept getting the format wrong but I was close. Thank you! – Church Sep 20 '12 at 00:41
4

strncpy is (essentially always) the wrong choice. In this specific case, it'll work, but only pretty much by accident -- since you're copying the end of a string, it'll work just like strcpy. That of course, leads to the fact that strcpy will work too, but more easily.

If you want to handle a more general case, there's a rather surprising (but highly effective) choice: sprintf can do exactly what you want:

sprintf(d, "%s", c+20);

sprintf (unlike strncpy or strcpy) also works for the most general case -- let's assume you wanted to copy just do I to d, but have d come out as a properly NUL-terminated string:

sprintf(d, "%*s", 4, c+13);

Here, the 4 is the length of the section to copy, and c+13 is a pointer to the beginning of the part to copy.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • last example, doesn't work for me. It returns `do I do this` not `do I d`. http://ideone.com/u8HRuI – mrdaliri Nov 20 '14 at 21:51
  • I agree that sprintf is the best choice (I use it for all string copying operations) but I think snprintf is even better. Call me paranoid... I've gone one step further and defined myself snprintf_safe whose return value is limited to only valid numbers in the range of 0 to n-1 (where n is the buffer size). I have found a class of bugs which use the return value from snprintf, since it can return values longer than the actual string which was written - useful in some situations but dangerous when you aren't expecting it... – Wayne Uroda Mar 16 '18 at 01:32
0

Sounds like you are looking for the strncpy function. Don't mind the C++ site, the documentation is for C.

Or you can also use the strcpy function.

mjgpy3
  • 8,597
  • 5
  • 30
  • 51
  • Beware: "No null-character is implicitly appended to the end of destination, so destination will only be null-terminated if the length of the C string in source is less than num." – John Carter Sep 20 '12 at 00:31
0

Use memcpy which may be a better fit, can only copy a portion of a string.

Using strncpy or strcpy actually copies a whole string up to the '\0' terminator.

In any case, it is prudent and wise, to assume there is plenty of space for the resulting string otherwise buffer overflows so ensure there is sufficient space in the buffer.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110