I just read that we need to give the type of pointers while declaring them in C (or C++) i.e.:
int *point ;
As far as I know, pointers sto
You need to specify the type as the standard demands so. Moreover, so that there are no issues when you try to perform pointer arithmetic like addition or subtraction.
Because:
The last two points don't apply to void
pointers, which is why they cannot by dereferenced and no pointer arithmetic may be done on them. The standard specifies that a void
pointer must be big enough to hold any kind of pointer (except function pointers, which are a different story altogether) and that assignment to and from void
pointers may be made without casts (at least in C, in C++ casts are always needed).
While processors often have different instructions for "load a byte from an address", "load a 16-bit halfword from an address", and "load a 32-bit word from an address", and likewise for "store" operations, C uses the same syntax to load a byte from an address as to load any other size value. Given the statement:
int n = *p;
the compiler may generate code which loads a byte, halfword, or word from the address in p and store it into n; if p is a *float, it may generate a more complicated code sequence to load a floating-point value in c, truncate it, convert to int, and store the converted value into n. Without knowing the type of p, the compiler can't know which operation would be appropriate.
Likewise, the statement p++
may increase the address in p
by one, two, four, or some other number. The amount by which the address is increased will upon the declared type of p. If the compiler doesn't know the type of p, it won't know how to adjust the address.
It is possible to declare a pointer without specifying the type of the thing to which it points. The type of such a pointer is void*
. One must convert a void*
to a real pointer type before doing anything useful with it, however; the primary usefulness of void*
lies in the fact that if a pointer is converted to void*
, it may be passed around as a void*
by code which knows nothing about the pointer's actual type. If the pointer is eventually given to code which does know its type, and that code casts the pointer back to that type, the result will be the same as the pointer that had been converted to void*
.
Code which will have to handle pointers to things it knows nothing about can often usefully employ void*
for such purposes, but code which does know about the things to which pointers point should generally declare pointers of the proper type.
The type of pointer comes to play while dereferencing and pointer arithmetic. For example
int x=10; //Lets suppose the address of x=100
int *ptr=&x; //ptr has value 100
printf("The value of x is %d", *ptr);
ptr++; // ptr has value 104(if int is 4bytes)
In the above example the pointer type is int so the compiler will start looking for the values stored in the next 4 bytes(if int is 4bytes) starting from memory address 100. So the type of pointer tell the compilers that how many bytes its should look for while dereferencing. If the pointer type was not there how would the compiler would have known that how many bytes to look while dereferencing. And when we did ptr++ the type of pointer tells by how much the ptr should be incremented. Here ptr is incremented by 4.
char c='a'; //Lets suppose the address of c = 200
char* ptr=&c; //ptr has value 200
ptr++; //ptr has value 201(char assumed to be 1 byte)
The pointer type tells that ptr is incremented by 1 byte.