i am passing a& (memory address of char a) so it is has a type or b (address of type int)
void pointer supposed not to point to any type but we see clearly &a which is reference to
address of a which is TYPE char and b WITH TYPE int so its not exactly void pointer isnt it?
maybe im wrong in the all understanding of this concept please i need explanation
thanx in advance |
There are no special memory addresses for different types. Each address is the location of 1 byte of memory. A char is 1 byte, so it takes up 1 byte of memory. Char variables stored sequentially in memory, will also then just be stored in sequential memory each one byte and each addresses of the next char, increased in value by 1.
An int is 4 bytes, so it will take up 4 bytes, and so 4 memory addresses.
There is a special thing which is done for you by the compiler, which is to take into account the type. An example is pointer arithmetic.
In assembly, which C++ is compiled into, if you wanted to fetch a 4 byte value from memory, you give an instruction to get 4 bytes starting at the address you would consider to be it's address in C++, but you're really getting all the bits from 4 separate sequential memory addresses starting at that location.
So the compiler, knowing the type generates correct assembly for you.
C++ compilers make sure you are passing, as an argument, the correct type, as you have specified in your declaration. The type of the parameter must match the type of the argument. But if the parameter is a void pointer, then it is not associated with a type. It's type is void. You can cast a typed pointer as a void pointer, you should cast it first just to be explicit, or it will be implicitly casted to a void pointer. But when this happens, you lose all of the special actions from the compiler which implement the way your type information translates into assembly. So you need to cast it back to the type you wanted, otherwise the compiler does not have the information it needs to translate the code in a surely correct way according to what you may have intended.
So it's no longer pointing to any type, just to a 1 byte memory address in which you now only have information given that it is where the first byte (8 bits) or possibly the entire 8 bits of some value you are interested in are at.