Thread: [Rookie] Defining Arrays

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    I'm just bumfuzzled as to how to initialize all these new char values without making my code 20 lines longer
    Programming isn't about line lengths though. It is easy for me to accept that simple things might stretch 50 or more lines, if I have enough reasons. Either because it looks cleaner or because I have many use cases to consider. Take a moment to appreciate how complex you've made the task already -- passcode handles not just password input, but it plays a role in controlling the loop. If you start worrying about line numbers you'll just make things impossible for yourself.

    Normally a C string that contains 8 characters would only handle a 7-character long password. You need room for the terminating '\0' character.

    Also, be aware of when you are comparing a single character:

    if ( passcode[0] == (char)-1 )

    or a whole string:

    int result = strcmp( passcode, "someword" );

    The semantics of these operations are different.

  2. #17
    Registered User
    Join Date
    Nov 2011
    Saratoga, California, USA
    Gads. -1 != "-1" != '-1' ... but then there is no char literal '-1' ! Perhaps the single-quotes was a misleading mistake.

    Either way, the user has no way to to cause a char to take on a -1 integer value when being read in as a string.
    if(passcode == negOne)
    will not work the way you expect it to. This will evaluate the first character in the passcode string with the value of -1, which it can't be as I stated.

    In your case, the user can enter -1 but it will be the string "-1", and you will need to use strcmp(), not the equality operator, ==, to determine if the strings are equal.

    char passcode[ 8 ] = {0};
    A string is always terminated by the '\0' character, which is equivalent to zero, so it is non-printable. In other words, there is a character on the end of every string that you do not see when printing. The bottom line being that you need to account for that extra character which will be appended to your string.

    char maxlim = 99999999;
    char minlim  = 10000;
    Even a 2 byte char would have an upper limit of 65,536. Again, a misunderstanding of char vs string (array of chars). I think what you intended was
    char maxlim[] = "99999999";
    char minlim[]  = "10000";
    Then you would have strings to compare to your passcode string. But here's the rub,
    strcmp( "99", "19999999" )
    will return a value indicating that 99 > 199999999 because it is comparing ASCII values, on a char by char basis. So, while this works to check for "-1", it will not help you in your range test.

  3. #18
    Registered User
    Join Date
    May 2012
    char negOne = -1;
    Whether char is signed or unsigned is implementation dependent. Thus you should explicitly declare negOne as signed char if you want to use negative numbers.

    char maxlim = 99999999;
    char minlim = 10000;
    char is only 1 byte, thus you can only use 256 different values (0..255 for unsigned char, -128..127 for signed char).

    scanf("%s", passcode);
    A safer way to avoid buffer overflows is to use a width specifier:
    scanf("%7s", passcode);  // see whiteflags' response why passcode can only store 7 characters
    if(passcode == negOne)
    You can't compare a string (passcode) with a char (negOne). I also don't see a reason to use a sentinel value. I would tell the user to quit by entering a non-number and then just check if the first character is a digit.

    In general, if you use the string/char array method, you have to be aware that the value of a char is its ASCII code (ignoring other encodings), thus the character '1' hasn't the numerical value 1 but 49.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rookie looking for help!
    By Newklear in forum C Programming
    Replies: 28
    Last Post: 03-11-2011, 05:15 AM
  2. Arrays: defining a new class in c++
    By jeremy-hulse in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2010, 08:41 AM
  3. Defining char arrays as string literals
    By Programmer_P in forum C++ Programming
    Replies: 23
    Last Post: 06-06-2009, 07:18 AM
  4. Replies: 9
    Last Post: 01-26-2008, 03:12 AM
  5. Help a rookie
    By elrookie in forum C Programming
    Replies: 14
    Last Post: 06-14-2007, 10:28 AM

Tags for this Thread