Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring type of pointers?

Tags:

c++

c

pointers

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 store the address of variables, and address occupies same amount of memory whatever may be the type. So, why do we need to declare its type?

like image 716
CodeBlaster Avatar asked Dec 05 '13 18:12

CodeBlaster


People also ask

What is pointer type declaration?

A pointer declaration names a pointer variable and specifies the type of the object to which the variable points. A variable declared as a pointer holds a memory address.

How many types of pointer we can declare?

There are majorly four types of pointers, they are: Null Pointer. Void Pointer. Wild Pointer.

How a pointer is declared in C?

The * symbol indicates that the variable is a pointer. To declare a variable as a pointer, you must prefix it with *. In the example above, we have done a pointer declaration and named ptr1 with the data type integer.

What is the type of this pointer?

The type of the this pointer for a member function of a class type X , is X* const . If the member function is declared with the const qualifier, the type of the this pointer for that member function for class X , is const X* const . A better technique would be to declare member mutable.


2 Answers

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.

like image 66
Mike Seymour Avatar answered Sep 21 '22 00:09

Mike Seymour


Because:

  1. addresses to different types don't need to have the same size. The standard explicitly specifies so (C 2011 standard (online draft), 6.2.5/28).
  2. type-safety: this allows the compiler to detect when you provide an incompatible pointer to a function, or in an assignment. This in turn prevents ugly situations where you mess up the argument order to a function.
  3. the compiler needs to know the type when the pointer is dereferenced.
  4. to do pointer arithmetic the size of the object pointed to needs to be known and thus its type.

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).

like image 31
Kninnug Avatar answered Sep 23 '22 00:09

Kninnug