开发者

What is this pointer in C?

I have seen a lot of these types, but i have no idea what it means

unsigned char somevar[MAXLEN];
int *pt开发者_如何学Cr = (int *) somevar;

Can someone explain?


Essentially you're interpreting an array of chars as a pointer to int. Suppose sizeof (int) is 4 and the char array contains these bytes:

b0 b1 b2 b3 b4 b5 b6 b7 

Now ptr will point to b0 but threat it as an int. That is,

ptr[0] is the integer comprised of bytes b0, b1, b2, and b3
ptr[1] is the integer comprised of bytes b4, b5, b6, and b7

HTH


It's just that somevar will be interpreted as a sequence of int's (or just one) through the ptr pointer. Incrementing ptr just once moves the pointer sizeof(int) bytes ahead.

Be aware of endianness when doing conversions like these. The bytes from somevar may need reordering to be properly interpreted as ints.

Also make sure that somevar is of a length that's a multiple of sizeof(int), otherwise you would get undefined behaviour when trying to access the last int since it will be partially available.


The identifier of an array in C is a pointer to the array's first element. So, in your example, somevar is an unsigned char * pointing to the first element of the array declared in the first line of the snippet.

And so it is then quite clear that ptr is also a pointer to the first element of the array, but thanks to the typecast it views it as a signed integer instead.

That's not necessarily a good typecast, there's a good chance that ints and chars are a completely different size and this could lead to some undefined behaviour.


This is often done when you read some raw bytes from, say, a binary file or off a network socket, and you know (from the data format or protocol) that these sequence of bytes represent an integer. This would give you a pointer that you can dereference and get the integer value represented by those bytes.


On the right hand side it is a pointer to an int.

On the left hand side it is a variable (somevar) of unknown type being cast into a pointer to an int. One would be well advised to look at somevar with a critical eye. Sometimes casting in C is needed; but, more often casting in C in an indication that the wrong type for somevar was chosen or that you're doing something that is not likely to be portable.

In this case, you are packing chars into ints. Depending on the system, you may arrive at an int with the internal bit representation which matches [char0, char1, char2, char3] followed by another int with the internal bit representation that matches chars four through seven.

However, on some other systems, you might arrive with an int with the internal bit representation of [char3, char2, char1, char0]. Finally there are other systems which handle bit order even more differently.

Odds are good that if you dig through the code enough, you'll find that there is another place where the pointer to your "constructed" int gets cast back into a char*.


It is a pointer to the first value of the array. Since It is an unsigned char it would need to be type cast as int(well it does not need to be but it is good practice). As per someone concern over size a size int will for the most part always be smaller than an unsigned char.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜