What is this pointer in C?

I have seen many of these types, but I have no idea what this means.

unsigned char somevar[MAXLEN]; int *ptr = (int *) somevar; 

Can someone explain?

+4
source share
6 answers

It is just that somevar will be interpreted as an int sequence (or just one) using the ptr . The ptr increment only moves the sizeof(int) byte pointer forward once.

Keep in mind with conversions like these. Bytes from somevar may need reordering to be interpreted as ints.

Also make sure that somevar is a multiple of sizeof(int) , otherwise you will get undefined behavior when trying to access the last int , as it will be partially accessible.

+1
source

Essentially, you interpret the array of characters as a pointer to an 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 threaten them as int . I.e

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

NTN

+3
source

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

So, it’s now clear that ptr is also a pointer to the first element of the array, but thanks to the typecast method, it treats it as a signed integer.

This is not necessarily a good type, there is a good chance that ints and chars are completely different, and this can lead to some undefined behavior.

0
source

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

0
source

On the right side is a pointer to an int.

On the left side is a variable (somevar) of unknown type, passed to a pointer to an int. It would be good to advise looking at someone with a critical eye. Sometimes casting in C is required; but, most often, casting in C indicates that the wrong type was selected for somevar or that you are doing something that is unlikely to be portable.

In this case, you are wrapping characters in ints. Depending on the system, you can get an int with an internal bit representation that matches [char0, char1, char2, char3], followed by another int with an internal bit representation that matches characters four through seven.

However, on some other systems you can get with int with the internal bit representation [char3, char2, char1, char0]. Finally, there are other systems that handle the order of bits even more differently.

The odds are good in that if you dig the code enough, you will find that there is another place where the pointer to your "built" int returns to char *.

0
source

This is a pointer to the first value of the array. Since this is an unsigned char, it should be cast as int (well, it is not necessary, but it is good practice). Since someone is concerned with size, the size of an int will for the most part always be less than an unsigned char.

0
source

All Articles