Converting int to unsigned char *.
Author Message
Converting int to unsigned char *.

Hi folks,

When performing the following, what exactly happens:

{
int number = 10;
unsigned char *uc = (unsigned char *) &number;

Quote:
}

More precisely, if "number" has a trap representation, will this be passed
along to "uc" since unsigned char preserves "number"s bit pattern?  OTOH,
isn't unsigned char guaranteed by the standard to not have a trap
representation (thus constituting the only type in C where this is so)...?
If that's the case, does the second point invalidate the first?  Or am I
completely missing something?

Help in sorting this out is greatly appreciated.

Mon, 04 Jul 2005 17:23:38 GMT
Converting int to unsigned char *.
* Marco Soeima
| More precisely, if "number" has a trap representation, will this be passed
| along to "uc" since unsigned char preserves "number"s bit pattern?

A bit pattern which is a trap representation in one type is not
necessarily a trap representation in another type.

| OTOH, isn't unsigned char guaranteed by the standard to not have a
| trap representation (thus constituting the only type in C where this
| is so)...?

Unsigned char is guaranteed to have no trap repsentations.  If char is
unsigned then it won't have any trap representations, either.

Jeremy.

Mon, 04 Jul 2005 17:24:03 GMT
Converting int to unsigned char *.

Quote:

> Hi folks,

> When performing the following, what exactly happens:

> {
>         int number = 10;
>         unsigned char *uc = (unsigned char *) &number;
> }

> More precisely, if "number" has a trap representation, will this be passed
> along to "uc" since unsigned char preserves "number"s bit pattern?

You never assign number to a unsigned char.

OTOH,

Quote:
> isn't unsigned char guaranteed by the standard to not have a trap
> representation (thus constituting the only type in C where this is so)...?

You never assign to an unsigned char so this is not an issue.

Quote:
> If that's the case, does the second point invalidate the first?  Or am I
> completely missing something?

You are missing the point that you are assigning a pointer to int to a
pointer to unsigned char. This is perfectly legal. When you do this you
can walk the int byte by byte via incrementing the pointer.

But if number above has a trap representation you have a problem
before the assignment occurs anyways. In the above code snippet
it is not possible for number to have a trap representation.

--
Thomas.

Mon, 04 Jul 2005 18:24:29 GMT
Converting int to unsigned char *.

Quote:
>When performing the following, what exactly happens:

>{
>        int number = 10;
>        unsigned char *uc = (unsigned char *) &number;
>}

uc is initialised to point to the first byte of number.  The way number
was initialised is entirely irrelevant to the issue.

Quote:
>More precisely, if "number" has a trap representation, will this be passed
>along to "uc" since unsigned char preserves "number"s bit pattern?

You seem to be missing the & operator in the initialiser of uc.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

Mon, 04 Jul 2005 21:16:24 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages