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

using (char*) in switch statment

Hello everybody.
I am new in C programming language and have an (if satament) and need to transfer it into switch statment.
My problem is that I have a field named (node_kind) of char* type and I compare it's content in the if statment using (strcmp) but I don't know how to do that in the switch statment.
Would you plz tell me how ?
Here is a short quote of my program

Expand|Select|Wrap|Line Numbers
  1. if (strcmp(node->node_kind, "VAR_TOKEN_e") == 0)
  2.         job = visitor->visitjob_VAR_TOKEN;
  3.     if (strcmp(node->node_kind, "INT_e") == 0)
  4.         job = visitor->visitjob_int;
  5.     if (strcmp(node->node_kind, "BOOL_e") == 0)
  6.         job = visitor->visitjob_bool;
Oct 16 '11 #1
3 2590
donbock
2,426 Expert 2GB
The short answer is that you can't. The switch and case variables are all compared as int's. If you need to compare a type other than int (or that can't be promoted to int) then use if/else if/else.

If it turns out that either of the arguments to your strcmp's are single-character strings then strcmp is only examining a single char ... and a char can be promoted to an int. However, it does not look like you are using single-character strings.
Oct 17 '11 #2
johny10151981
1,059 1GB
donbock is right in his way.

But you can convert it to switch statement.
Simple way would be.
get a new variable, Integer will be perfect for this.
and then assign the value of integer under the same condition you have made and then you just need to use your integer number in your switch, so far I think, entire process is pointless
Oct 17 '11 #3
@johny10151981
Do you mean that I assign each value of the (node_kind) field to an integer value and then compare using it or what !!?
Oct 17 '11 #4

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

Similar topics

6
by: Charlie | last post by:
Dear all, I am stuck on this problem, couldn't find an answer on any C++ book in the bookshop. Following is a cut of my program, and its error message:...
10
by: Ramprasad A Padmanabhan | last post by:
Hello all, On my linux box ( redhat 7.2 ), I have been using char as a boolean data type. In order to save on the number of bytes as compared to using int or short int. typedef char boolean;...
32
by: Conan1st | last post by:
Hello everyone! I made a variable under unsigned short but displaying it using char the value that doesnt change in fact is shown changed... unsigned short a a = 321;
4
by: Andrea Williams | last post by:
I used to program with VBS and I'm used to allowing multiple cases in one CASE line. Anyway to do that C#? VB Example: Select Case (lngStatus) Case 1,2,3 'do something Case 4 'do something
12
by: shalakasan | last post by:
Hi, I am creating a pointer to char: char * lDirName = new char; I am trying to set values character by character. lDirName= 'c'; lDirName= ':'; lDirName= '\\'; The problem is that, the...
2
by: Amanda | last post by:
If I do this, i get complaints saying that || cannot be used for char and bool. Any help? // options will be a,b,c,or e do { Console.Write("Enter a selection from the following list:\n" +...
4
by: huzzaa | last post by:
I am using a switch statement that will decide from a random number what message to display. I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to...
2
by: tonesgirl85 | last post by:
I need some direction to know if I am on the right track. Please contique program below. import java.util.*; public class Roman { static Scanner console = new Scanner(System.in);
6
by: asincero | last post by:
Which is better: using an if/else construct to simulate a C switch or use a dictionary? Example: def foo(): if c == 1: doCase1() elif c == 2: doCase2() elif c == 3: doCase3()
7
by: Brice Rebsamen | last post by:
Reading the code from showkey.c (from package kbd) I found this type of code: char *m; m = "RAW"; See below for the complete code. How can this work? I would have used strdup, or allocation...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.