R
rouble
Hi All,
Is it safe to store a uchar in a void* and then extract the uchar value
out of it again ?
My understanding is that the size of a void* should always be equal to
or greater than the size of a uchar. So, theoretically, this should be
safe. Please correct me if I am wrong.
I've got the following code that compiles (with warnings), and it also
works as desired.
3 int
4 main ()
5 {
6
7 unsigned char a = 'A';
8 void *b = NULL;
9 unsigned char c;
10
11 printf("\na is %c\n", a);
12
13 b = (void*) a;
14 printf("\nb is %x\n", b);
15
16 c = (unsigned char) b;
17 printf("\nc is %c\n", c);
18 }
I get the following warnings, for obvious reasons:
~/strings #5 > gcc void.c
void.c: In function `main':
void.c:13: warning: cast to pointer from integer of different size
void.c:16: warning: cast from pointer to integer of different size
Is there anyway to get rid of these warnings (without using memcpy) ?
I rewrote the code as follows:
3 int
4 main ()
5 {
6
7 unsigned char a = 'A';
8 void *b = NULL;
9 unsigned char c;
10
11 printf("\na is %c\n", a);
12
13 memcpy(&b, &a, sizeof(unsigned char));
14 printf("\nb is %x\n", b);
15
16 memcpy(&c, &b, sizeof(unsigned char));
17 printf("\nc is %c\n", c);
18 }
No warnings, and it works as before.
Are there any gotchas as to the safety or portability of this code ?
TIA,
rouble
Is it safe to store a uchar in a void* and then extract the uchar value
out of it again ?
My understanding is that the size of a void* should always be equal to
or greater than the size of a uchar. So, theoretically, this should be
safe. Please correct me if I am wrong.
I've got the following code that compiles (with warnings), and it also
works as desired.
3 int
4 main ()
5 {
6
7 unsigned char a = 'A';
8 void *b = NULL;
9 unsigned char c;
10
11 printf("\na is %c\n", a);
12
13 b = (void*) a;
14 printf("\nb is %x\n", b);
15
16 c = (unsigned char) b;
17 printf("\nc is %c\n", c);
18 }
I get the following warnings, for obvious reasons:
~/strings #5 > gcc void.c
void.c: In function `main':
void.c:13: warning: cast to pointer from integer of different size
void.c:16: warning: cast from pointer to integer of different size
Is there anyway to get rid of these warnings (without using memcpy) ?
I rewrote the code as follows:
3 int
4 main ()
5 {
6
7 unsigned char a = 'A';
8 void *b = NULL;
9 unsigned char c;
10
11 printf("\na is %c\n", a);
12
13 memcpy(&b, &a, sizeof(unsigned char));
14 printf("\nb is %x\n", b);
15
16 memcpy(&c, &b, sizeof(unsigned char));
17 printf("\nc is %c\n", c);
18 }
No warnings, and it works as before.
Are there any gotchas as to the safety or portability of this code ?
TIA,
rouble