some_id
some_id

Reputation: 29886

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 xmlChar has the value of 30, I need this in int form.

Upvotes: 1

Views: 1254

Answers (2)

Douglas Leeder
Douglas Leeder

Reputation: 53310

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?

Upvotes: 0

JSBձոգչ
JSBձոգչ

Reputation: 41378

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);

Upvotes: 2

Related Questions