473,246 Members | 1,429 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,246 software developers and data experts.

char comparison error

196 100+
Hi, im just trying to compare a character(char) to a set list of characters (e.g. "a", "b", etc).

first i read in the value- (i have tried this two ways)
Expand|Select|Wrap|Line Numbers
  1. scanf("%c", &var_name);
  2. //also tried this and yes i know the difference
  3. scanf("%s", &var_name);
When i printf this value -
Expand|Select|Wrap|Line Numbers
  1. printf("%c this is var_name", var_name);
It outputs what it is supposed to,

Now for the actual error (of which there are a couple)

When i compare var_name to another character
Expand|Select|Wrap|Line Numbers
  1. char var_name;
  2. if (var_name != "z") {
  3. // code goes here
  4. }
I get the following error cannot compare a const char to a char.

Then i read somewhere about adding an * to fix this problem (i thought this was a pointer by the way) -

Expand|Select|Wrap|Line Numbers
  1. char *var_name;
  2. if (var_name != "z") {
  3. // code goes here
  4. }
And believe it or not it actually worked???, anyhow now its stopped working completely (e.g even though var_name = "z" it wont pick it up)

So my question to anyone who will help how do i compare a char to a character and have it work.

Any help in this matter is greatly appreciated, you have my thanks.
Apr 6 '08 #1
2 1645
671 Expert 512MB
On the code that involves replacing %c with %s (at the beginning):
Interesting. I thought you would have removed the ampersand from var_name, because you are passing in an array, right?

But on to your actual problem. Just as a note, when something doesn't work, don't guess the code. You'll typically make things worse (as you did here).

First, I'll note the problems in your two code snippets involving character comparisons. In the first snippet, the issue is using "z" instead of 'z'. Yes, the quote type matters. "z" creates a C string. 'z' creates a character.

The error message is a bit cryptic if you are a beginner in C. So, var_name is a char. That's simple. Now look at "z". First, it's a constant. It's a constant as much as if you had typed the number 5 in place of it. Second, a C string can be pointed to with a char*. You have a constant string literal, so you have a const char*. And you try to compare that to a char. Hence the compilation error. You actually wanted the char 'z'. So use the single quotes.


But your second snippet is even worse. So you added a * to var_name, and now you have a character pointer. This gives you an immediate problem. Suddenly, you no longer have a character. So what you have to do is allocate memory for a character, and then point var_name to it. I'm betting you omitted this portion in your code.

Of course, the var_name != "z" comparison will pass. You are comparing two char pointers. It's always going to return false though. "z" occupies a certain place in memory. var_name isn't going to point to it. That is what comparing two pointers does, by the way. It compares if the pointer values are equal. So when you have

Expand|Select|Wrap|Line Numbers
  1. char *cstr1, *cstr2;
  2. ... cstr1 == cstr2 ...
That comparison doesn't check if the string contents match. You need to explicitly go through the string contents and check if each individual character matches up with one another. That's why there is a function in the library strcmp and strncmp to do so for you.

But you never wanted a string comparison anyway. Change "z" to 'z' and you're set to go.
Apr 6 '08 #2
196 100+
Thanks for the help, it is greatly appreciated
Apr 6 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: muser | last post by:
is string converted to its integer equivalent by minusing it by 48? the function is suppose to check the fifth digit of struct member using the formula contained within the function. The function...
by: Francis Bell | last post by:
Hello, I've got a program that is reading in a data file of 25 lines. Here is an example of the first two lines: sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1 f/floating minnow/AAA...
by: Neil Zanella | last post by:
Hello, Consider the following program. There are two C style string stack variables and one C style string heap variable. The compiler may or may not optimize the space taken up by the two stack...
by: Marcus | last post by:
I'm struggling with a bit of code that maybe some of you could help me understand. This code processes character values from streaming data. I'm trying to get my head around being able to get at...
by: twkelsey | last post by:
Hi, My company has a scenario where we would like to change the data type of an existing primary key from an integer to a char, but we are concerned about the performance implications of doing...
by: Vishal Ladha | last post by:
Hi ! I have been experimenting with char * for a while now. I have two pieces of code : Code1 : ===== char *ptr = "hello";
by: aruna.mysore | last post by:
Hi all, I have a simple definitioin in a C file something like this. main() { char a; ....... int k; }
by: subramanian100in | last post by:
Suppose we have char *a = "test message" ; Consider the comparison if (a == "string") ..... Here "string" is an array of characters. So shouldn't the compiler
by: HED | last post by:
I am learning c++, and there is a lot that i dont know, so if this is a stupid question, I am sorry. regardless, I am trying to get my program to check and see if a char value is a certain value,...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.