very simple array question

Discussion in 'C Programming' started by Michael, Jul 22, 2006.

  1. Michael

    Michael Guest

    Hi,

    I have the following in some code:

    char temp[MAX_INPUT_LENGTH];

    .....stuff....

    for(i = 0; i < MAX_INPUT_LENGTH; i++){
    if(temp == ":"){
    ...stuff....
    }

    but when I have a string with ':' in it, the 'if' doesn't match. Why?

    Thanks for your help

    Michael
    Michael, Jul 22, 2006
    #1
    1. Advertising

  2. Michael

    code_wrong Guest

    "Michael" <> wrote in message
    news:44c21d56$0$23495$...
    > Hi,
    >
    > I have the following in some code:
    >
    > char temp[MAX_INPUT_LENGTH];
    >
    > ....stuff....
    >
    > for(i = 0; i < MAX_INPUT_LENGTH; i++){
    > if(temp == ":"){
    > ...stuff....
    > }
    >
    > but when I have a string with ':' in it, the 'if' doesn't match. Why?
    >


    ":" is a string ... (it has a : and a string terminating null character)
    you need to use single quotes for it to be treated as a single character
    like this: ':'
    code_wrong, Jul 22, 2006
    #2
    1. Advertising

  3. Michael

    jacob navia Guest

    Michael a écrit :
    > Hi,
    >
    > I have the following in some code:
    >
    > char temp[MAX_INPUT_LENGTH];
    >
    > ....stuff....
    >
    > for(i = 0; i < MAX_INPUT_LENGTH; i++){
    > if(temp == ":"){
    > ...stuff....
    > }
    >
    > but when I have a string with ':' in it, the 'if' doesn't match. Why?
    >
    > Thanks for your help
    >
    > Michael
    >
    >


    You are the victim of one of C's pitfalls.

    Actually what you mean is
    if (temp == ':')

    Note the single quotes. That means the character ':'. You have written
    ":" , and that is a pointer to a constant character string of
    two places:
    The first is ':' and the second is zero.

    Since you are comparing a character with a pointer your compiler should
    have complained and you decided to ignore the complaint.

    Change the double quotes into single quotes and it will work
    jacob
    jacob navia, Jul 22, 2006
    #3
  4. On Sat, 22 Jul 2006 20:43:04 +0800, in comp.lang.c , "Michael"
    <> wrote:

    >Hi,
    >
    >I have the following in some code:
    >
    >char temp[MAX_INPUT_LENGTH];
    >
    >....stuff....
    >
    >for(i = 0; i < MAX_INPUT_LENGTH; i++){
    > if(temp == ":"){
    > ...stuff....
    > }
    >
    >but when I have a string with ':' in it, the 'if' doesn't match. Why?


    Because ":" is not the same as ':'.

    The first is an array of two chars - ':' and null
    The second is a single char, ':'


    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jul 22, 2006
    #4
  5. "Michael" <> writes:
    > I have the following in some code:
    >
    > char temp[MAX_INPUT_LENGTH];
    >
    > ....stuff....
    >
    > for(i = 0; i < MAX_INPUT_LENGTH; i++){
    > if(temp == ":"){
    > ...stuff....
    > }
    >
    > but when I have a string with ':' in it, the 'if' doesn't match. Why?


    As others have pointed out, temp is a char, and ":" is a string
    (which decays to a pointer to char in this context).

    Trying to compare a char and a char* for equality is a constraint
    violation, requiring a diagnostic (at least a warning, more likely a
    fatal error).

    If your compiler didn't complain, you're using it in a mode where it
    doesn't warn you about serious errors. Find out how to increase its
    diagnostic level. Your compiler can catch a lot of errors for you if
    you let it.

    (If it did complain, then your compiler already answered your
    question, and you shouldn't have needed to ask us. If you didn't
    understand the error message, perhaps something like "warning:
    comparison between pointer and integer", you could have shown it to
    us.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 22, 2006
    #5
  6. Michael

    Blueboy Guest

    Keith Thompson wrote:
    > "Michael" <> writes:
    > > I have the following in some code:
    > >
    > > char temp[MAX_INPUT_LENGTH];
    > >
    > > ....stuff....
    > >
    > > for(i = 0; i < MAX_INPUT_LENGTH; i++){
    > > if(temp == ":"){
    > > ...stuff....
    > > }
    > >
    > > but when I have a string with ':' in it, the 'if' doesn't match. Why?

    >
    > As others have pointed out, temp is a char, and ":" is a string
    > (which decays to a pointer to char in this context).
    >
    > Trying to compare a char and a char* for equality is a constraint
    > violation, requiring a diagnostic (at least a warning, more likely a
    > fatal error).
    >
    > If your compiler didn't complain, you're using it in a mode where it
    > doesn't warn you about serious errors. Find out how to increase its
    > diagnostic level. Your compiler can catch a lot of errors for you if
    > you let it.
    >
    > (If it did complain, then your compiler already answered your
    > question, and you shouldn't have needed to ask us. If you didn't
    > understand the error message, perhaps something like "warning:
    > comparison between pointer and integer", you could have shown it to
    > us.)
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.


    -----------------

    Ok! What you are doing is comparing a character with a string!

    Either use the String Compare Function or replace your
    replace your code

    from this:

    if(temp == ":")

    to this:
    if(temp == ':')
    Blueboy, Jul 24, 2006
    #6
  7. Michael

    Guest

    I got the answer to you;

    You have declared 'temp' as string(array of char) and comparing one of
    its character using "" which is again a string. i.e. each character in
    a string can be accessed using ' ' (single code mark) and not ""
    ..REPLY IF ANY DOUBT
    Michael wrote:
    > Hi,
    >
    > I have the following in some code:
    >
    > char temp[MAX_INPUT_LENGTH];
    >
    > ....stuff....
    >
    > for(i = 0; i < MAX_INPUT_LENGTH; i++){
    > if(temp == ":"){
    > ...stuff....
    > }
    >
    > but when I have a string with ':' in it, the 'if' doesn't match. Why?
    >
    > Thanks for your help
    >
    > Michael
    , Jul 25, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    448
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,589
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    407
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    495
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,244
Loading...

Share This Page