Kemoy Campbell
Kemoy Campbell

Reputation: 118

convert from unsigned char to char*

I have a function that currently returns unsigned char. However, I want to cast that unsigned char to a char* but I keep getting cast to/from pointer from/to integer of different size warning.

Let foo be the function that return an unsigned char

I tried something like this:

char* convert = (char*)foo();

The reason for converting to char* is because I need to use the strcasecmp function to perform a string compare.

Upvotes: 1

Views: 995

Answers (2)

Mark Segal
Mark Segal

Reputation: 5550

You can't cast a non-pointer function return value into a pointer at all. Think about it, where would this pointer point to?

You can, however, save the result to a variable and have a pointer to it. For example,

 char tmp = (char) foo();
 char * ptr = &tmp;

Edit:

As another answer said, if you want to use strcasecmp you would need a char array where the last byte is the null terminator.

Upvotes: 2

gnasher729
gnasher729

Reputation: 52530

To achieve what you want, use the following code:

char array [2];
array [0] = (char) foo ();
array [1] = '\0';
...
... strcasecmp (array, ...);

This builds an array containing a string of a single character which you can then use.

Upvotes: 2

Related Questions