Pointers

Why do we need different types of pointers when it occupies the same memory space for any type of pointers?

Questions by Nerdyravi

Showing Answers 1 - 12 of 12 Answers

akshay.deo

  • Aug 3rd, 2010
 

Though all the pointers occupy 4 bytes (32 bit compiler) they can point to wide range of data types. These data types have different structure for e.g int is 4 byte wide, char is 1 byte wide ... So manipulation of the corresponding pointers needs to be different. Like for  char * p... p++ will always move ahead by 1 byte and for int *p, p++ will move ahead by 4 bytes. 

It's *not* guaranteed that all pointer types take up the same amount of space or have the same alignment.  One example that comes to mind is a word-addressed system; accessing an individual char requires the word address plus an offset, so a pointer to char would be wider than a pointer to int.

  Was this answer useful?  Yes

mohamr2

  • Sep 20th, 2010
 

Its not that we require different pointer, actually pointer are of the same size depending on the target platform that the memory belongs to. It is the pointer type that matters, we require an int pointer to specify that the data that the pointer points to is an int... similarly for char... hence to identify what kind of data is actually pointed by the pointer we use different pointer types...

  Was this answer useful?  Yes

supriti.dan

  • Nov 30th, 2010
 

Pointer is a special kind of variable which can hold the address of other
variable. As the address of any variable is a integer that's why pointer
variable itself is always of integer type but in case of pointer variable
declaration, we use different kind of data type to refer that particular pointer
can hold the address of mentioned data type.

Example: char b='w';
/> char *a=&b;
that means a is a pointer variable which is able to hold the
address of any char variable.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions