简体   繁体   English

使用枚举常量时发生C错误

[英]C error while using enum constants

I want to write a function that will take a string which represents a hexadecimal number and converts it to an integer, I want to put the hexadecimal digits in an enum, but when I use an element from the enum I get an error while compiling. 我想编写一个函数,该函数将采用代表十六进制数字的字符串并将其转换为整数,我想将十六进制数字放入枚举,但是当我使用枚举中的元素时,在编译时会出错。

Here is the code: 这是代码:

#include <stdio.h>

int htoi (char h[]);
enum HexDigits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F};

int main () {
    enum HexDigits h = 9;

    return 0;
}

int htoi (char h[]) {

}

and here is the error im getting: 这是我得到的错误:

C:\\Users\\KiKo-SaMa\\Desktop\\C>gcc hello.c -o hello C:\\ Users \\ KiKo-SaMa \\ Desktop \\ C> gcc hello.c -o hello

hello.c:4:17: error: expected identifier before numeric constant hello.c:4:17:错误:数字常量之前的预期标识符

What could be the problem with what I'm doing? 我在做什么可能是什么问题?

What an enum does is to assign symbolic names to what is essentially an integer, by default starting at index 0. For example; 枚举的作用是将符号名分配给本质上为整数的名称,默认情况下从索引0开始。

enum BoolValue { FALSE, UNKNOWN, TRUE };

essentially does something close to; 本质上是接近

const int FALSE   = 0;
const int UNKNOWN = 1;
const int TRUE    = 2;

Your problem is that you're trying to put numbers in your enum instead of symbolic names, which makes sense that it gives an error if you rewrite it as essentially; 您的问题是,您试图将数字而不是符号名放入枚举,这很有意义,如果您将其本质上重写为错误;

const int 0 = 0;
const int 1 = 1;

...etc. ...等等。

I'm not sure an enum is a solution to the problem you're trying to solve. 我不确定枚举是否可以解决您要解决的问题。

It is incorrect to use such a enum definition, it should be like this 使用这样的枚举定义是不正确的,应该是这样的

enum Hex
    {       
        ZERO = 0,
        ONE = 1,
        A = 10,
        B = 11,
    };

if your problem is to convert string to int use atoi 如果您的问题是将字符串转换为int,请使用atoi

Enum members have to be named and their names (like any other name) aren't allowed to start with a digit. 枚举成员必须被命名,并且他们的名字(和其他名字一样)不允许以数字开头。 If you want to use digits, you have to add something in front, eg a letter or an underscore (eg H0 or _0 ). 如果要使用数字,则必须在前面添加一些内容,例如字母或下划线(例如H0_0 )。

Also, as an additional suggestion, it's a lot easier to simply add your valid characters into a string and then use strchr() for conversion: 另外,作为其他建议,将有效字符简单地添加到字符串中,然后使用strchr()进行转换要strchr()

const char *hexchars = "0123456789ABCDEF";

// this takes a single hex character and returns its decimal representation (or -1 in case of an invalid character)
int hex_to_dec(char h) { return strchr(hexchars, h) ? strchr(hexchars, h) - hexchars : -1; }

Also note that you might as well just use the hexadecimal representation of numeric constants: 0x0 , 0x1 ,... 0xF . 另外请注意,你可能也只是用数字常量的十六进制表示: 0x00x1 ,... 0xF

Use strtol() to convert strings from any number base 2-36. 使用strtol()可以转换任何以2至strtol()为底的数字的字符串。

strtol(const char *nptr, char **endptr, int base)

Base 0 is a special case that accepts "0x" prefix for base 16, "0" prefix for 8, and no prefix for 10. 基数0是一种特殊情况,它接受基数16的“ 0x”前缀,8的“ 0”前缀和10的前缀。

char a[5]="0x0a";
char b[3]="0b";
printf ("%li\n",strtol(a,NULL,0));
printf ("%li\n",strtol(b,NULL,16));

See the man page for longer examples with error checking. 有关错误检查的更多示例,请参见手册页

enum HexDigits h = 9; // assigning numerical 9 or enum variable

the 9 in this case can be taken as the numerical 9 and the '9' in enum HexDigits. 在这种情况下,数字9可以作为枚举HexDigits中的数字9和'9'。

The Variables used inside enum definition should follow the variable naming conventions of the language. 枚举定义中使用的变量应遵循该语言的变量命名约定。 Only valid variable names are allowed. 仅允许使用有效的变量名。

But why do you have to define enum?? 但是为什么要定义枚举呢? Doesnt c++ support Hexadecimal numbers on its own? c ++本身不支持十六进制数字吗?

check out about enum at MSDN page MSDN页面上查看有关枚举的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM