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
So that it can perform arithmetic and other operations. Consider these two examples:
int* p; /* let the address of the memory location p pointing to be 1000*/
p++;
printf("%u",p); /* prints 1004 since it is an integer pointer*/
char *p; /* let the address of the memory location p pointing to be 1000*/
p++;
printf("%u",p); /* prints 1001 since it is an char pointer*/
I hope this helps you !
We actually don't need (see below) to declare the type, but we should. The pointer stores information about the objects location, while the type defines how much space it takes in memory.
The size of the object stored at the pointed memory is needed in various cases - array creation, assigment, copying the memory, and finally - creating the object using new
.
However you can still define a void
pointer, if you want to hide (for any reason) the type:
void* dontKnowWhatTypeIsHere;
A void pointer is considered an universal one. It can point to any object, and when we want to use it with a type, we'll just do reinterpret_cast
.
One reason is in pointer arithmetic. You cannot do p+1
unless you know the size of the element to which p
points -- that is the size of the type to which p
is a pointer. If you'd try p+1
on a void *p
you're likely to get a bad answer (it is the same as if done on a char *
but maybe you didn't want that; it is caught by -pedantic
as a warning and by -pedantic-errors
as an error).
Another reason is type safety. If a function receives as argument an int *
you cannot pass a pointer to char
(a string) there. You'd get a warning (an error with -Werror
/ -pedantic-errors
). Consider this (dummy) code:
void test(int *x)
{
}
int main()
{
char *x = "xyz";
test(x);
return 0;
}
Compiling (using gcc (GCC) 4.8.2 20131017 (Red Hat 4.8.2-1)
) gives:
1.c: In function ‘main’:
1.c:8:2: warning: passing argument 1 of ‘test’ from incompatible pointer type [enabled by default]
test(x);
^
1.c:1:6: note: expected ‘int *’ but argument is of type ‘char *’
void test(int *x)
^
So, why do we need to declare its type?
You want to know the type of the pointer so you can do static type checking.
We also need to know the type in order for pointer arithmetic to work, for example when we index into an array(which is equivalent to pointer arithmetic) of different size types the pointer will be adjusted by a type dependent amount. If we look at the draft C99 standard section 6.5.6
Additive operators says (emphasis mine):
For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to an object type [...]
So the pointer needs to be an object type, which means not incomplete or void.
You also said:
address occupies same amount of memory whatever may be the type. So, why do we need to declare its type?
This is not always true in C++ the size of pointers to member functions can change depending on the class type, one of the good articles that covers this is Pointers to member functions are very strange animals.
Furthermore we can see that both the C99 draft standard section section 6.2.5
Types paragraph 27 which says:
[...] Pointers to other types need not have the same representation or alignment requirements.
and the draft C++ standard section 3.9.2
Compound types paragraph 3 says:
[...] The value representation of pointer types is implementation-defined. Pointers to cv-qualified and cv-unqualified versions (3.9.3) of layout-compatible types shall have the same value representation and alignment requirements (3.11). [...]
do not require pointers to have the same representation except in specific cases.
Lots of aforesaid statement but apan is purely right. Now your question why we define type of pointers? First definition of pointer A pointer which can hold the address of another variable. The above definition is partial. The exact definition is pointer is a variable which can hold the address of a variable and if we de-reference(fetch the value) it, it will returns the value of the present on that address. If a pointer is fails to returns a value on dereferencing then it is not a pointer. You can try that even in gcc compiler a simple variable can also hold the address of another variable but at dereference it will give you error. Now the size Irrespective of data types the size of pointer is always equal to the size of integer on that specific compiler. So the size of pointer in gcc compiler is 4bytes(size of integer) and in turboc its size is 2bytes(size of integer). Now question is why equal to size of integer. What will be the address of any variable it may be int, char, float etc the address is always a whole integer number and where the whole integer number is stores in int. That's why the size of pointer is equal to the size of int because it also stores the address which is always a pure integer data. Then what is the difference between an int and char of any other data types pointer. At the time of retrieval your compiler will fetch the number of bytes according to your data types other wise you will get error or not an error but for you some unpredictable result but not for me. Same rule apply for increment and decrement the pointer it always increment and decrement according to pointer data types. The size of pointer doesn't depend on the data type and hence the reason that your link list come into picture because if you try to declare the structure inside same variable then you will get compile time error because your compiler doesn't the size of structure before its complete declaration but self referential pointer of same structure are allowed why? The only answer because the size of pointer doesn't depend the size of data type. If you any query then please ask me. Thanks asif aftab
Type-safety. If you don't know what p
is supposed to point to, then there'd be nothing to prevent category errors like
*p = "Nonsense";
int i = *p;
Static type checking is a very powerful tool for preventing all kinds of errors like that.
C and C++ also support pointer arithmetic, which only works if the size of the target type is known.
address occupies same amount of memory whatever my be the type
That's true for today's popular platforms. But there have been platforms for which that wasn't the case. For example, a pointer to a multi-byte word could be smaller than a pointer to a single byte, since it doesn't need to represent the byte's offset within the word.