Thread: Checking value of one character in array

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    Checking value of one character in array

    Very simple problem here, I just don't understand why this isn't working.

    Code:
    if(current_dir[(strlen(current_dir) - 3)] == '.')
    given that current_dir is:
    /cygdrive/c/dirent_tests/fssw.exe

    the if statement should be true, right? No matter what I do it's always false, what's up?

    -System_159

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There are 33 characters + a '/0', so strlen gives 33. The '.' character is current_dir[29], so it looks like you should subtract 4.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tabstop View Post
    There are 33 characters + a '/0', so strlen gives 33. The '.' character is current_dir[29], so it looks like you should subtract 4.
    Also, this code seems to assume that all extensions are three letters long. Better to use strrchr() to locate the last '.' in the string. Everything to its right is the extension.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by brewbuck View Post
    Also, this code seems to assume that all extensions are three letters long. Better to use strrchr() to locate the last '.' in the string. Everything to its right is the extension.
    The only extensions I'll be using are 3 characters long, which is why I've done it this way.

    tabstop, thanks for pointing that out(again). Hopefully from now on I'll remember the damn '/0' !

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by System_159 View Post
    The only extensions I'll be using are 3 characters long, which is why I've done it this way.
    "The only bullets I'll be firing are blanks, so that's why I'm pointing the gun at my head."


  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    There are 33 characters + a '/0', so strlen gives 33. The '.' character is current_dir[29], so it looks like you should subtract 4.
    I believe that's '\0'.
    '/' is not an escape character to my knowledge.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Location
    Fengcheng,China
    Posts
    1
    Quote Originally Posted by Elysia View Post
    I believe that's '\0'.
    '/' is not an escape character to my knowledge.
    You're right,absolutely

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    By the way, this is not due to the fact the last character of a string is '\0' but because the first element of an array is at index 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. watching character array in visual studio
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2006, 11:12 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Determine the size of a character array...
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-10-2002, 10:22 AM