Reputation: 1458
I have an unsigned char* type and want to assign it an integer value. How can I do it in C?
Upvotes: 0
Views: 12893
Reputation: 399803
Just do it, with an appropriate cast:
unsigned char *pointer = (unsigned char *) 0xdeadf00d;
This does exactly what you asked for, it assigns an integer value to a pointer to unsigned char
. This is not a very useful thing to be doing, but that's how you do it.
Of course there's no requirement (that I'm aware of) that this is even possible; your architecture's idea of a pointer might not support this, in which case I guess the compiler will tell you.
Upvotes: 4
Reputation: 8197
#include <stdio.h>
int main()
{
unsigned char *ucrp,*ucr2p;
unsigned char ucr2='X';
int Integr_val=320;
ucr2p=&ucr2 ;
ucrp=(unsigned char *)&Integr_val;
*ucr2p= Integr_val;
return printf("\n %c,%i\n %c,%i",*ucrp,*ucrp,*ucr2p,*ucr2p);
}
With this example you can basically understand what loss you are performing.A char without explicit typecasting can convert an integer value.
Output:-
@,64
@,64
Upvotes: 0