very simple array question

M

Michael

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
 
C

code_wrong

Michael said:
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: ':'
 
J

jacob navia

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
 
M

Mark McIntyre

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
 
K

Keith Thompson

Michael said:
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.)
 
B

Blueboy

Keith said:
Michael said:
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.)


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

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 == ':')
 
G

guru.jois

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top