length of char array

This is a discussion on length of char array within the C Programming forums, part of the General Programming Boards category; Hi say i have a char array of size 19 (meaning 20 characters can be input) now i got a ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    length of char array

    Hi
    say i have a char array of size 19 (meaning 20 characters can be input)

    now i got a problem, if i input more then 20 characters it will store them still and be used for my other variables (overflow)

    Is there a way to find how many characters a char array is holding?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    say i have a char array of size 19 (meaning 20 characters can be input)
    If you are using it as a string and the size is 19, only 18 characters can be input, since the last char would be the terminating null character.

    Unless the array has decayed to a pointer (e.g., you passed it as an argument), sizeof() could be used to find the size.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    oh ok so like this:
    char name[20] = abc
    if (sizeof(name) > 20)
    output error

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    or is there anyway to say if more than 20 characters have been inputted dont store the rest? how do i prevent the overflow
    Last edited by taurus; 09-16-2007 at 12:43 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by taurus View Post
    oh ok so like this:
    char name[20] = abc
    if (sizeof(name) > 20)
    output error
    that will always return 20. you want to use strlen.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Or just don't ask for more than 20?
    Code:
    char name[20];
    
    fgets(name, sizeof(name), stdin);

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by robwhit View Post
    that will always return 20. you want to use strlen.
    By the time strlen is applied, it's too late. The key is stopping before you've read too much, as zacs shows.
    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

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    yes and thats what i dont know how to do?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    use fgets. see zacs7's post.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    54

    hey

    Hey taurus

    have you finished the psuedocode yet???

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    oh alright so let me see if i understand this:
    the user can enter a name, now his statement checks if its below 20 characters and then if it is stores

    OR

    does it work by only storing 20 characters and ignore the rest inputed there?

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    fgets(str, 20, stdin); reads 19 and leaves the rest in the stdin buffer.

    unless a newline is read first.
    Last edited by robwhit; 09-16-2007 at 09:29 PM.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    but say i have more variables after that, it wont write the excess in the buffer to those variables?

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    only if you read it in with an input function.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    and yea theres the problem, i am going to be inputting other things, problem?

Page 1 of 3 123 LastLast
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, 05:13 PM
  2. Replies: 16
    Last Post: 11-23-2007, 12: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, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21