Thread: A quick question on comparing characters.

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    16

    A quick question on comparing characters.

    Hello

    For some reason this gives an exception error in my compiler (VS.NET). I have no idea why i cant test a character like this. I'm a little rusty at the moment but i didnt realise i was this rusty!


    Code:
    char *pointer_String = "A1";
    
    if (pointer_String[0] == 'A')
    {
           printf("It matches!\n");
    }
    Any help would be great!
    Thanks very much!

    Garry

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    That code works perfectly for me when compiled with DevC++/gcc

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    does it work?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    As it stands, looks good

    On the other hand, if you had this
    if (pointer_String[0] = 'A')

    that would be very wrong
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    I did use the '=='.
    Its strange, the program crashes when it gets to the if statement.
    Any ideas?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not without you post the whole program, or a cut-down one which still shows the problem.

    Random side effects elsewhere within the program can sometimes have strange effects on what seems to be perfectly reasonable code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (C/C++)
    By owi_just in forum C Programming
    Replies: 2
    Last Post: 03-19-2005, 09:44 AM
  2. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  3. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  4. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 02:54 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM