0

How to convert char* to enum

Help me

9th Nov 2021, 11:48 AM
venkatesh rapeti
venkatesh rapeti - avatar
11 Réponses
+ 6
Those are two different things really. One is a pointer type, the other is an enumerated constants. What is on your mind that you think this was useful?
9th Nov 2021, 11:53 AM
Ipang
+ 4
Tag the language you're using. Your answer might be language specific. But as far as I know, you can't convert a char* to an enum. Like, imagine it. Integer to double conversion makes sense, you're just adding the decimal storing capacity to it. But a char* is a pointer to a character, and an enum is a completely different type(usually used to store integers). How can you convert between them, or why would you want to convert between a char* and an enum
9th Nov 2021, 11:54 AM
Rishi
Rishi - avatar
+ 2
In one function I hv char * (means string) argument I assign that argument to enum So I ask that sir
9th Nov 2021, 11:56 AM
venkatesh rapeti
venkatesh rapeti - avatar
+ 2
In which language? In C or C++, enums can only store integer numbers, no strings Or you might be misunderstanding the situation. Share your code so that we can help you more efficiently
9th Nov 2021, 11:57 AM
Rishi
Rishi - avatar
+ 2
venkatesh rapeti in C++ as I said, enum variables can only store integers. Share your code, let's see what actually is needed there
9th Nov 2021, 12:00 PM
Rishi
Rishi - avatar
+ 2
#include <stdio.h> #include <stdbool.h> bool fun(char *first, char *second); enum USAGE { ON, OFF, Kill }; int main() { char str1[10]; char str2[10]; printf("Enter input : "); scanf("%s",str2); fun(str1,str2); printf("%s\n",str1); return 0; } bool fun(char *first, char *second) { USAGE e = static_cast<USAGE>(*second); switch(static_cast<int>(e)) { case 'ON': first="ON"; printf("%s",first); return true; break; case 'OFF': first = "OFF"; printf("%s",first); return true; break; case 'Kill': first = "KL"; printf("%s",first); return true; break; default: first = "NONE"; printf("%s",first); return false; } }
9th Nov 2021, 12:09 PM
venkatesh rapeti
venkatesh rapeti - avatar
0
C++
9th Nov 2021, 11:59 AM
venkatesh rapeti
venkatesh rapeti - avatar
0
K
9th Nov 2021, 12:01 PM
venkatesh rapeti
venkatesh rapeti - avatar
0
venkatesh rapeti I see you have created something like an object with the enum definition. As far as I know, we can't do this right? Or maybe I don't know. But in this tutorial about how to use enums in C++, I can't find anything like this. Even I referred some other websites too https://www.tutorialspoint.com/how-to-use-enums-in-cplusplus
9th Nov 2021, 3:46 PM
Rishi
Rishi - avatar
- 1
venkatesh rapeti Okay somehow it is working. Though I don't know about this, I can tell you a way to work around this. Why don't you try to compare the string input and assign your value accordingly to the enum variable manually? My idea is like the below snippet (consider it doesn't have any errors) USAGE e; if(strcasecmp(second, "on")==0) { e=ON; } else if(strcasecmp(second, "off")) { e=OFF; } else { e=Kill; } Try this and tell if it worked. All the best =)
9th Nov 2021, 3:53 PM
Rishi
Rishi - avatar
- 1
Thank u for responding
9th Nov 2021, 4:20 PM
venkatesh rapeti
venkatesh rapeti - avatar