铸造指向整数的指针

时间:2011-04-15 12:12:15

标签: 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);

我收到了这个错误

  

架构x86_64的未定义符号:     “_main”,引自:         从crt1.10.6.o开始     “_commandfetcher”,引自:         ccPv5Pvd.o中的_commandFetcher   ld:找不到架构x86_64的符号

如何将xmlChar指针强制转换为int?例如,xmlChar的值为30,我需要以int形式。

2 个答案:

答案 0 :(得分:2)

您不能简单地将char投射到int。 (或者更确切地说,你可以,但它没有做你认为它做的事。)

使用strtol将字符串转换为整数:

char* number = "30";
int value = strtol(number, NULL, 0);

答案 1 :(得分:0)

你不想转换指针 - 你想取消引用它。

但在这种情况下,您可能希望将字符串转换为整数?

相关问题