473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

very simple array question

Hi,

I have the following in some code:

char temp[MAX_INPUT_LENGTH];

.....stuff....

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

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

Thanks for your help

Michael
Jul 22 '06 #1
6 1128

"Michael" <mi*********@yahoo.comwrote in message
news:44***********************@per-qv1-newsreader-01.iinet.net.au...
Hi,

I have the following in some code:

char temp[MAX_INPUT_LENGTH];

....stuff....

for(i = 0; i < MAX_INPUT_LENGTH; i++){
if(temp[i] == ":"){
...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: ':'
Jul 22 '06 #2
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[i] == ":"){
...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[i] == ':')

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
Jul 22 '06 #3
On Sat, 22 Jul 2006 20:43:04 +0800, in comp.lang.c , "Michael"
<mi*********@yahoo.comwrote:
>Hi,

I have the following in some code:

char temp[MAX_INPUT_LENGTH];

....stuff....

for(i = 0; i < MAX_INPUT_LENGTH; i++){
if(temp[i] == ":"){
...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
Jul 22 '06 #4
"Michael" <mi*********@yahoo.comwrites:
I have the following in some code:

char temp[MAX_INPUT_LENGTH];

....stuff....

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

but when I have a string with ':' in it, the 'if' doesn't match. Why?
As others have pointed out, temp[i] 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) ks***@mib.org <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.
Jul 22 '06 #5

Keith Thompson wrote:
"Michael" <mi*********@yahoo.comwrites:
I have the following in some code:

char temp[MAX_INPUT_LENGTH];

....stuff....

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

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

As others have pointed out, temp[i] 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) ks***@mib.org <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[i] == ":")

to this:
if(temp[i] == ':')

Jul 24 '06 #6
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[i] == ":"){
...stuff....
}

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

Thanks for your help

Michael
Jul 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: 1 | last post by:
hi all i need to have a page on a clients site that has a competition question that changes weekly. they need to be able to change the question through some kind of admin page (i was going to...
13
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
2
by: Tomaz Rotovnik | last post by:
How can I get array of unsigned char from VC++ dll to VB For example: I declared next function in VC++ dll: int funct(int size, unsigned char* array) I want to read the contents of array in...
4
by: Geoff Cox | last post by:
Hello, No doubt this is simple but I cannot see how to do it ... I have a variable called situation_number with a series of values 1, 2, 3 etc. I would like to have a series of variables...
10
by: Jason Curl | last post by:
Greetings, I have an array of 32 values. This makes it extremely fast to access elements in this array based on an index provided by a separate enum. This array is defined of type "unsigned long...
6
by: Omid Hodjati | last post by:
Hi All, I implemented an encryption algorithm using C#, native C++ and managed C++. Then I measured the CPU time used for executing this algorithm in all implementation. The managed version of...
2
by: purna chandra | last post by:
Hello, I have a simple question.Hoping not to take much of your valuable time...:-). I am trying to get the data from a string, and am wondering if I get...
4
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
6
by: Ronald Raygun | last post by:
I want to be able to randomly select the following from an array: 1). An image 2). A piece of text (name of tge image) 3). A piece of text (description of the image) I want to be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.