将指针转换为整数

标签 c pointers casting

我的源文件中有一些 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/

相关文章:

c# - Enumerable.Cast<T> 是否复制对象?

c - C语言中如何清除缓冲区?

c - 如何在C中将十六进制字符串(带 float )转换为二进制?

c - 修剪 char * 中的前 n 个字符并释放它

c - 在函数中初始化的数组指针在返回后包含错误值

c - 需要这个 "extra"转换吗?

c - 为什么我不能在 ai_socktype 中使用 SOCK_RAW?

c - Xlib + Unity 仅关闭允许的操作

C指向结构中数组的指针传递给函数

c - 预递增的字符指针在递增后都指向相同的地址?