Thread: length of char array

  1. #16
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    See the FAQ for flusing the input buffer then, http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  2. #17
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by juststartedC View Post
    Hey taurus

    have you finished the psuedocode yet???
    Taurus your class mate is asking you somenthing LOL

    ssharish

  3. #18
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    You have to understand that user input is kept in a buffer called stdin. It is dynamic and resizes itself so you don't have to worry about making things fit into it. When you store 20 bytes (characters) of data into an array you are using a function to read 20 bytes from stdin and store them into the array. If you don't tell it to read 20 bytes (if you use a function like scanf() where the array size is not passed to the function, it will read bytes from stdin until it encounters a NULL or a byte represented in hex as 00 meaning the end of the string). If you understood nothing I just said, just remember to use fgets and specify the length of your array and you will never run into problems concerning overflow.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) The data is read from a stream, not a buffer. A buffer may be in there, but it is irrelevant to the concept of reading from a stream.
    2) You can tell scanf() the buffer size:
    Code:
    scanf("%20s", buffer);
    It even works for dynamically sized buffers:
    Code:
    scanf("%*s", size, buffer);
    3) The limit character for fgets() is the newline, '\n', not the NUL. A text stream will in fact never emit a NUL; at best it will emit an EOF.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by CornedBee View Post
    It even works for dynamically sized buffers:
    Code:
    scanf("%*s", size, buffer);
    Hmm? In the scanf family, the * is for assignment suppression.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Oh, sorry. In that case, you have to create a format string.
    Code:
    sprintf(buffer1, "%%%is", size);
    scanf(buffer1, buffer2);
    Stupid omission, IMO.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    So how big do you make buffer1? [/nitpick]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Quote Originally Posted by keira View Post
    You have to understand that user input is kept in a buffer called stdin. It is dynamic and resizes itself so you don't have to worry about making things fit into it. When you store 20 bytes (characters) of data into an array you are using a function to read 20 bytes from stdin and store them into the array. If you don't tell it to read 20 bytes (if you use a function like scanf() where the array size is not passed to the function, it will read bytes from stdin until it encounters a NULL or a byte represented in hex as 00 meaning the end of the string). If you understood nothing I just said, just remember to use fgets and specify the length of your array and you will never run into problems concerning overflow.
    ahhhhhhh i see, so instead of using all that buffer thing i can use fgets?

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Dave_Sinkula View Post
    So how big do you make buffer1? [/nitpick]
    ceil(log10(UINT_MAX)) + 3
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #25
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by CornedBee View Post
    ceil(log10(UINT_MAX)) + 3
    So how ugly does the scanf version get if you want its fgets equivalent?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Very ugly. I never said it was nice, only that it was possible.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #27
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    jus a quick question. Is there a command to erase the buffer in C. say i enter a string. now i can say if string>20 then erase buffer and reinput?

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean like this FAQ question:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    ya i was doing the buffer thing: scanf("%20s", buffer);

    but once i enter more than 20 characters i get a segmentation fault. How can i create an error message instead?

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    ya i was doing the buffer thing: scanf("%20s", buffer);

    but once i enter more than 20 characters i get a segmentation fault. How can i create an error message instead?

    Yes, becasue scanf is "stupid" and can't deal with things going wrong very well - there is A REASON we suggest using fgets() instead - it is more fault-tolerant and unless you do things wrong, you can't make it overwrite other variables and such.

    But unfortunately, you are in a class where you can't use that [unless I'm confused], so you have to live with the application not being particularly fault-tolerant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 06:13 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM