Thread: strcpy get errors

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I've provided two examples in which gets is used. The program knows the exact number of characters that are on stdin preceding the new line in both cases, so there is absolutely zero harm in using it in both of those cases. A size parameter is unnecessary in both of the examples I've provided, just as it's unnecessary when you can verify that the destination buffer is large enough to store the source string in a call to strcpy. You shouldn't use gets when it introduces bugs in your programs, but that is not always the case, as I've already demonstrated.Although, there is one reason for why you should avoid the use of gets entirely, and that's because it's no longer part of the standard C library.edit: If you can come up with another reason (independent of the context in which it's used), then I'd like to hear it. Otherwise you'd just be repeating what I've said.
    Last edited by Barney McGrew; 01-17-2013 at 08:12 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Barney McGrew View Post
    I've provided two examples in which gets is used. The program knows the exact number of characters that are on stdin preceding the new line in both cases, so there is absolutely zero harm in using it in both of those cases. A size parameter is unnecessary in both of the examples I've provided, just as it's unnecessary when you can verify that the destination buffer is large enough to store the source string in a call to strcpy. You shouldn't use gets when it introduces bugs in your programs, but that is not always the case, as I've already demonstrated.Although, there is one reason for why you should avoid the use of gets entirely, and that's because it's no longer part of the standard C library.edit: If you can come up with another reason (independent of the context in which it's used), then I'd like to hear it. Otherwise you'd just be repeating what I've said.
    Fair enough.

    Your examples don't cover* unbuffered input streams (like one would find on a microcontroller), but I think that you've proven your point

    [edit]*I think that "don't cover" is incorrect - I meant to say "will never work with"[/edit]
    Last edited by Click_here; 01-17-2013 at 08:59 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using strcpy
    By laxkrzy in forum C Programming
    Replies: 1
    Last Post: 11-15-2010, 11:09 PM
  2. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  3. What's up with this strcpy?
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 05:24 PM
  4. strcpy
    By Tibo in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 07:02 AM
  5. strcpy
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2002, 01:39 PM