int myInteger = 5;
void* myVoidPointer = &myInteger; // myVoidPointer now stored the adress of myInteger
int mySecondInteger = myVoidPointer*; // error, cannot convert void to int
int myThirdInteger = (int*)myVoidPointer*; // should work fine
Pointer arithmetic is undefined for a void pointer, so it should throw a syntax error.All pointers are exactly the same size, that being the size of your CPU architecture, so either 32 or 64 bits these days. The only difference between int*, char*, and void* is what they point to is different, and that comes into play when you try to dereference the pointer or do pointer arithmetic.
For example, say you declare an int array and then declare a pointer, "int* ptr", that contains the address of the first element of the array. If you do "ptr++", you will now be pointing to the second element of the array. If, on the other hand, ptr is a void*, then you will only increment the address by one byte, and you'll be pointing into the middle of the first element, instead.
A void pointer allows you to have a pointer that you may not know what kind of data it is going to point to at compile time. Of course, that means that your code will have to deal with it at run time.and if use void pointer then what is advantages and dis-advantages.
Hmm, maybe it depends on the compiler. GCC treats a void* exactly like a char* for the purpose of arithmetic, it increments it one byte.Pointer arithmetic is undefined for a void pointer, so it should throw a syntax error.
If you set the options of gcc to be ANSI-compliant, it will throw an error. But gcc (and some other compilers) allow pointer arithmetic on void pointers as a non-standard extension. The result is code that compiles on gcc and then won't compile (or will compile but behave differently) on another compiler.Hmm, maybe it depends on the compiler. GCC treats a void* exactly like a char* for the purpose of arithmetic, it increments it one byte.
Undefined means that the implementation is free to choose how to handle it and not all GCCs will increment it by one byte. It is usually treated as the smallest unit of addressable memory in the architecture which is not always one byte. In some DSPs for example it is 16-bits (2 bytes). Then on those architectures char is normally 2 bytes also so it is almost always treated as char* but not guaranteed.Hmm, maybe it depends on the compiler. GCC treats a void* exactly like a char* for the purpose of arithmetic, it increments it one byte.