我的源文件中有一些 xmlChar *,我需要它们是整数形式。
如何正确转换这些?
当我尝试这个 world->representation = malloc(sizeof(int *) * mapHeight);
它说
error: invalid operands to binary * (have ‘long unsigned int’ and ‘xmlChar *’)
当我尝试这个的时候
world->representation = malloc(sizeof(int *) * (int) mapHeight);
我遇到了这个错误
Undefined symbols for architecture x86_64: "_main", referenced from: start in crt1.10.6.o "_commandfetcher", referenced from: _commandFetcher in ccPv5Pvd.o ld: symbol(s) not found for architecture x86_64
如何将 xmlChar 指针转换为 int?例如,xmlChar 的值为 30,我需要它的 int 形式。
最佳答案
您不能简单地将 char
转换为 int
。 (或者更确切地说,你可以,但它不会像你想象的那样。)
使用strtol
将字符串转换为整数:
char* number = "30";
int value = strtol(number, NULL, 0);
关于将指针转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676530/