A pointer is an abstraction that allows you to reference data in memory so that access to raw memory is used as an atomic block to ensure that it is interpreted appropriately for the selected type.
The pointer itself is represented by its own machine word size. In your example, you have two pointers to different types, but they are still pointers to addresses in memory, and therefore they are the same size. As mentioned in other answers, in order to get the size of the data type that the pointer points to, you must dereference it in a sizeof operation, for example. SizeOf (* p).
The ++ operator allows you to get a pointer that refers to the next address in memory for the corresponding type. If he simply increased the address by one byte for all types, you could get an address in memory that points to the middle of the data view
eg. for two unsigned 4-byte integers representing decimal values 1 and 4 278 190 080, respectively, starting from the address 0x00 in memory (note that the address here is just for illustration and does not represent a real system, since the OS reserves them for its own goals) / p>
address 0x00 0x01 0x02 0x03 | 0x04 0x05 0x06 0x07 data value (4 byte integer) 0x00 0x00 0x00 0x01 | 0xFF 0x00 0x00 0x00
If the pointer to the integer has a reference to the address 0x00, and the ++ operator simply increased the address of the pointer by 1 byte, you would have a pointer to the address 0x01, which, if you accessed this address (plus the following 3 bytes), as an integer , you get the integer represented by the data bytes 0x00 0x00 0x01 plus the value of the address 0x04, which in this case is the value 0xFF. This will result in an integer with a decimal value of 511, which is not one of the two integers stored in memory.
For proper access to the next integer in memory, the ++ operator must increment the pointer byte address by 4 bytes.
source share