1

In C++, mentioning the type of pointer is necessary. For example,

int a = 5;
int * p = &a;

Here, 'int' in the 'int *' was necessary. The reasons I found are that its needed for dereferencing.(Knowing no. of bytes to read, and their interpretation) and Pointer Arithmetic.

However, if I write:-

int a = 5;
char * p = &a;
float * p2 = &a;
//etc

Error is shown, saying that a char*,float* etc. pointer can't be assigned to an int variable. Ok.., so the compiler Knew the type of variable for whom I wanted a pointer for. This is unlike normal variables, where compiler doesn't know exactly which type I want. For example:-

int a = 5;
float b = 5;
double c = 5;
long d = 5;
char e = 5;

All these are perfectly fine. I need to write the type here as compiler won't know which type I want, as all of these would work.

However, in case of pointers, it knows the exact type of variable for which I want to declare a pointer to.

Even if I use explicit typecasting, still, I will be in a way telling the type of pointer I need, and still will have to specify the correct pointer type as well.

int a = 5;
char * b = (char*)&a;//This would work.
//However this,
int * b = (char*)&a;
//wouldn't, as even with typecasting, c++ knows exactly the type of pointer we need.

So, my question is, why doesn't C++ automatically set the type of pointer, when it knows the type of variable I am going to point to? When it can give error on not using the correct type of pointer, why not set it automatically?

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Rohan
  • 31
  • 3
  • 8
    Are you aware of the `auto` keyword? – chris May 24 '20 at 21:09
  • 6
    C++ type checking is a blessing, not a curse. – Paul Sanders May 24 '20 at 21:10
  • one possible reason is byte level manipulation of data. For example, you can have a void* pointer (pointing for some inner byte data) that can be acessed by *int or *char depending on how you want to read or write data on it. – imbr May 24 '20 at 21:13
  • 2
    Well, that's why you can instead write `auto * p = &a;` It does exactly what you want. It automatically sets the type of the pointer. – Nikos C. May 24 '20 at 21:13
  • 1
    Pointer types are not universally interchangeable - there are only some special cases. Regardless of the means of resolving (manual, auto, decltype), the exact type matters. Example on how the type of pointer variable trivially changes the behavior. int[] a = {1,2}; double* x = (double*)a; x += 1; int b = (int)*x; // ?? – user2864740 May 24 '20 at 21:17
  • possible related to why void* pointers are needed https://stackoverflow.com/questions/8530080/what-is-a-void-pointer-in-c – imbr May 24 '20 at 21:17
  • *"unlike normal variables, where compiler doesn't know exactly which type"* No, it does know. `5` is an `int`, and if you do `auto x = 5;`, `x` will be an `int`. The difference is that `int` has an *implicit conversion* to all those arithmetic types, but `T *` is not *implicitly convertible* to `U *` (assuming `T != U`, and they are not connected by inheritance). – HolyBlackCat May 24 '20 at 23:26

3 Answers3

8

If you declare b as auto, the compiler will use type inference and determine the type itself. But if you use a specific type, it has to be the correct one.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Note: The _type_ is still *necessary*, just not manually typing it out.. I avoid auto on trivial types and those with simple aliases as, eg., “auto*” is no simpler to type than “int*” and decreases transparency (and “auto” alone also loses the important visual pointer designator). 0.02 – user2864740 May 24 '20 at 21:29
  • Hi, thanks for the answer, I still have a doubt. What would be functioning of c++ would we miss if c++ would always automatically determine the type? – Rohan May 25 '20 at 06:49
  • 1
    Compile times are slower. The output binary should be literally the same. Sometimes having types in the code helps readability. When it does, prefer to use them. – Lou Franco May 25 '20 at 12:22
  • Thanks a lot, this has made the most sense to me till now. – Rohan May 25 '20 at 16:49
0

One possible reason is byte level manipulation of data.

For example, you can have a void* pointer (pointing tosome inner byte data) that can be acessed by *int or *char or whatever pointer you need depending on how you want to read or write data on it.

imbr
  • 6,226
  • 4
  • 53
  • 65
0

Mentioning the type indicates how your memory will store the data, in particular the number of memory cases, each type of data has its own size for example (see sizeof ()), working with low level languages sollicits attention on memory and hardware.

For example, the memory representation of a double is not the same as int, or any other type of data, even if they will print the same value as output (in case of implicite conversion), but in the stack, they are not completely the same "value". So the compiler will not allow this in case of pointers.

Using the keyword auto can manage what you are looking for.

rekkalmd
  • 171
  • 1
  • 12
  • Thanks for the answer. I actually mentioned the reasons. I know why the type is needed. What I want to know is that had c++ worked with just auto, what would go wrong? Or was it realised later and then the auto option was given? – Rohan May 25 '20 at 06:57