473,612 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char comparison error

196 New Member
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);
  4.  
When i printf this value -
Expand|Select|Wrap|Line Numbers
  1. printf("%c this is var_name", var_name);
  2.  
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
e.g.
Expand|Select|Wrap|Line Numbers
  1. char var_name;
  2. if (var_name != "z") {
  3. // code goes here
  4. }
  5.  
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. }
  5.  
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 1657
oler1s
671 Recognized Expert Contributor
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 ...
  3.  
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
chazzy69
196 New Member
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

18
1832
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 isn't doing that at the moment, hence this post and the querie above. string must be of type char. bool checkdigitforcustomercode( char* string ) {
12
2039
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 Lures/Skinny Minney/green/bass/0/0/0 In my program, I need to read in each line, BUT, I need to build a different object based upon the characters in the first field. So, I
2
2538
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 variables by placing them at the same address (my g++ compiler does this). Therefore the output of the given C program is compiler dependent. What is worse, the program does not do what its writer most likely intended, since, std::set's find()...
5
1945
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 the values of a const char as opposed to a char variable. The code is below: void GotLevel(const char *szLL, char MM) { char ss; _snprintf(ss, sizeof(ss) , "%s %c"
5
10423
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 so. The script for the two tables that we need to modify is listed below. Table FR_Sessions contains a column named TransmissionID which is currently an integer. This table contains about 1 million rows of data. Table FR_VTracking table also...
3
2873
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";
74
4543
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; }
9
5035
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
4
7273
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, and then act appropriately. The compiler goes into fits when I do this, and if I double click the error it opens ostream, and i figure that I really have no business messing with that. So, my code looks something like: choice is a char and...
0
8171
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6081
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4047
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4110
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2554
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.