Casting pointers to ints
I have a number of xmlChar * in my source file and I need them to be in integer form.
How does one cast these correctly?
When I tried this world->representation = malloc(sizeof(int *) * mapHeight);
it says
error: invalid operands to binary * (have ‘long unsigned int’ and ‘xmlChar *’)
and when I tried this
world->representation = malloc(sizeof(int *) * (int) mapHeight);
I got this error
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
How can I cast a xmlChar pointer to an int? e.g the 开发者_如何学CxmlChar has the value of 30, I need this in int form.
You can't simply cast a char
to an int
. (Or rather, you can, but it doesn't do what you think it does.)
Use strtol
to convert a string to an integer:
char* number = "30";
int value = strtol(number, NULL, 0);
You don't want to cast the pointer - you want to dereference it.
But in this case you probably want to convert the string into an integer?
精彩评论