|
In C, it is illegal to assign pointers of incompatible types. Void pointers are defined as a generic pointer type, but you can't dereference them. As an example, take the function memset() which sets a block of memory to the specified byte value. If the function was declared as
Code:
| | memset(int *s, int c, size_t n) |
you could only use it to fill arrays of integers, passing the address of an array of floats would be illegal and give a compiler failure. By declaring the function as
Code:
| | memset(void *s, int c, size_t n) |
it can accept pointers of any type without compilation errors. But as I said, inside the function you cannot dereference the pointer without giving it a type (eg. ((char*)s)[n] = c). As a sidenote, before the C89 standard there was no void type, and char pointers were used instead. AFAIK that is still legal, but discouraged.
The volatile keyword is a hint to the compiler that the object may be modified in a way that the compiler can't detect. What the keyword actually does is not specified, but with most compiler implementations it will prevent some optimizations as you noted. Typical uses are accesses to hardware registers and shared variables in multitasking environments. Note that volatile by itself may not be enough as caches etc. may interfere.
In C, static is used for two things: to specify lifetime and visibility. Static variables declared inside functions have global lifetime, ie. the same as the program. They are initialized once, before the program starts and will keep their value between calls of the function.
Functions and global variables declared static are not visible outside the file they are declared. The can be freely accessed by other functions in the same file, but if you try to access them from another file you will get a compiler error. You can have other static functions and variables with the same name in other files without getting conflicts. In C++, static is additionally used to declare static member variables (which are shared between all class instances) and functions (functions of the class that can be called directly and that cannot access any private data). |