0

I want to declare character array and later want to fill it. But getting error:

char line[BUFSIZE+1];
strcpy(line,"Memory is Full", sizeof(line));

Error is:

wrong number of arguments in call to strcpy. 

Is there any alternative to achive this?

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
Sijith
  • 3,740
  • 17
  • 61
  • 101

3 Answers3

3

If you have it, use strlcpy(). It does what you might expect strncpy() to do.

Failing that, I would recommend using snprintf(), if you have it.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • +1 for `snprintf` and `strlcpy`, but the latter is available only in BSD C libraries (and Solaris, IIRC, or was it AIX). – Fred Foo Oct 25 '12 at 14:09
2

strcpy() takes 2 arguments, strncpy() takes 3.

I think you were thinking about using strncpy, that takes destination, source, and size.

ie:

int main()
{
    char i[6];
    strncpy(i, "Hello", sizeof(i));
    printf("%s\n", i);    
    return 0;
}

>> ./a.out
>> Hello

Note: You have to append the '\0' yourself if you completly fill your char[], otherwise strncpy() will do it for you (as in my example).

Mike
  • 47,263
  • 29
  • 113
  • 177
  • 1
    But strncpy is a problematic, strange function. I wouldn't advise anyone to use it, memcpy() is a much safer and faster function. – Lundin Oct 25 '12 at 13:44
  • @Lundin What are the problems with strncpy? – simonc Oct 25 '12 at 13:46
  • In this case, problem with strncpy is that there is no guarantee the string is null-terminated. if BUFSIZE = 5, then line will contain 'Memory' and all other string functions will overrun the end of the string. – mlibby Oct 25 '12 at 13:48
  • @mcl - It just comes down to RTFM. It's clear everywhere, the man page for `strncpy()` even has a bold "warning" printed in it – Mike Oct 25 '12 at 13:52
  • @Mike, sorry I meant to respond only to simonc's question, not criticize your answer! I personally love `strncpy`, but Sijith's code needs to guard carefully against the length issue. Same problem applies if he uses `strcpy` or `memcpy`. – mlibby Oct 25 '12 at 13:57
  • 1
    @simonc Google for "problems with strncpy", "don't use strncpy", "strncpy seg fault" or find any debate about it on this site. There was one yesterday.. – Lundin Oct 25 '12 at 13:59
  • 1
    @Mike It's clear everywhere but in the standard: `If the array pointed to by s2 is a string that is shorter than n characters, null characters are appended to the copy in the array pointed to by s1, until n characters in all have been written.` They forgot to add: if the array pointed to by s2 >= n characters long, strncpy will cause your program to crash and burn. – Lundin Oct 25 '12 at 14:02
  • 2
    @simonc: see [this answer](http://stackoverflow.com/a/2115015/166749) (which is actually about `strlcpy`, but discusses `strncpy` as well). – Fred Foo Oct 25 '12 at 14:04
  • @Lundin - hmm, good point. Well, I covered it in this instance with my footnote, and a number of sources (apparently not everywhere though) do note the fact that the last null is missing when `s2 > n` – Mike Oct 25 '12 at 14:09
  • @Mike If you have to append '\0' manually, I think you are better off using memcpy() instead. The link posted by larsmans provides a very nice explanation of why strncpy is such a weird function: it was never intended to be a safe version of strcpy, it is merely a special-purpose function suitable for string types in ancient Unix systems. So unless you are writing code for an ancient Unix system, you should probably think twice before using strncpy. – Lundin Oct 25 '12 at 14:26
  • @Lundin - Doesn't memcpy have the same issue? If I `memcpy(dest, src, sizeof(dest))` and `src` is longer than `dest` you're still going to end up having to append the null yourself. I do agree about larsmans explanation and the `strlcpy` sounds like a good function. But as far as memcpy vs. strncpy, seems like a wash to me. – Mike Oct 25 '12 at 15:24
  • @Mike memcpy only does what you tell it to do. strncpy on the other hand, is looking for null terminations, and it may or may not append null terminations at the end, depending on whether you passed string length or buffer length, or something else. And it doesn't just add 1 null termination, it adds as many as it can. – Lundin Oct 25 '12 at 18:15
1

strcpy doesn't take a size/length argument. Use memcpy instead, but give it the size (length+1) of the string being copied, not the size of the buffer, or you risk undefined behavior/segfaults.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836