+ 2

How to declare multiple pointers in single code?

In dynamic array and pointer allocation

12th Aug 2018, 10:20 AM
Nahom Kassahun
Nahom Kassahun - avatar
2 Respuestas
+ 2
In both C and C++, the * binds to the declarator, not the type specifier. In both languages, declarations are based on the types of expressions, not objects. For example, suppose you have a pointer to an int named p, and you want to access the int value that p points to; you do so by dereferencing the pointer with the unary * operator, like so: x = *p; The type of the expression *p is int; thus, the declaration of p is int *p; This is true no matter how many pointers you declare within the same declaration statement; if q and r also need to be declared as pointers, then they also need to have the unary * as part of the declarator: int *p, *q, *r; because the expressions *q and *r have type int. It's an accident of C and C++ syntax that you can write T *p, T* p, or T * p; all of those declarations will be interpreted as T (*p). This is why I'm not fond of the C++ style of declaring pointer and reference types as T* p; T& r; because it implies an incorrect view of how C and C++ declaration syntax works, leading to the exact kind of confusion that you just experienced. However, I've written enough C++ to realize that there are times when that style does make the intent of the code clearer, especially when defining container types. But it's still wrong.
12th Aug 2018, 1:24 PM
Savan Patel
Savan  Patel - avatar
+ 1
thanks Savan i got exactly what i needed, i was kinda confused with the array one but now its great
13th Aug 2018, 12:08 PM
Nahom Kassahun
Nahom Kassahun - avatar