Correct way of declaring pointer variables in C/C++
Bjarne Stroustrup said:
The choice between "int* p;" and "int *p;" is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.
A "typical C programmer" writes "int *p;" and explains it "*p is what is the int" emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.
A "typical C++ programmer" writes "int* p;" and explains it "p is a pointer to an int" emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.
Source: http://www.stroustrup.com/bs_faq2.html#whitespace
I'd recommend the latter style because in the situation where you are declaring multiple pointers in a single line (your 4th example), having the asterisk with the variable will be what you're used to.
Pointer declaration in C, whats the difference?
Grammatically, the type uint8_t
is a declaration-specifier; whereas the pointer *
is a declarator. According to C's grammar declarators bind to the declared name rather than the declaration-specifier. Consequently your first line is parsed like:
uint8_t (*start_ptr), end_ptr;
i.e. only the first name becomes a pointer.
This is one reason some tend to put the space before the pointer *
rather than after. The correct way to declare those in one line would be:
uint8_t *start_ptr, *end_ptr; // both are pointers to uint8_t.
How can I declare a Pointer to a struct in C?
It doesn't matter where you put the spaces. struct Card* p
and struct Card *p
are equally valid; it's just a matter of style as to which one you prefer.
I agree the second form is more common, but what's most important is that you use one form consistently throughout your code. Your boss / teacher / other developers may also have coding style standards that specify which form to use.
In C, what is the correct syntax for declaring pointers?
It is simply a matter of how you like to read it.
The reason that some people put it like this:
Type *instance;
Is because it says that only instance is a pointer. Because if you have a list of variables:
int* a, b, c;
Only a is a pointer, so it's easier like so
int *a, b, c, *d;
Where both a and d are pointers. It actually makes no difference, it's just about readability.
Other people like having the * next to the type, because (among other reasons) they consider it a "pointer to an integer" and think the * belongs with the type, not the variable.
Personally, I always do
Type *instance;
But it really is up to you, and your company/school code style guidelines.
What is the correct way to declare a pointer to a __far pointer?
I believe you would do this:
unsigned int * __far *VariableThreePtrPtr;
A far pointer to a far pointer would be:
unsigned int * __far * __far VariableFourPtrPtr;
In C, does it matter where I put the * in a pointer variable declaration?
Basically, there's no difference between them.
But I prefer this method:
int *p;
Instead of:
int* p;
or
int * p;
The reason is that because if you want to declare a number of int *
in the same line, it makes more sense to have the *
next to the identifier instead of the type because that looks cleaner and less confusing. e.g.
int *p, *q, *r;
is better than:
int* p, *q, * r;
If you don't like my reasons, make sure you stick to one of them and don't vary the usage.
Related Topics
What's the Difference Between _Pretty_Function_, _Function_, _Func_
Dynamic_Cast and Static_Cast in C++
How to Reassign the Reference in C++
C++ Pass an Array by Reference
How to Return Local Array in C++
Best Way to Store Currency Values in C++
Is the Safe-Bool Idiom Obsolete in C++11
How Does the Capacity of Std::Vector Grow Automatically? What Is the Rate
Why Can't I Initialize Non-Const Static Member or Static Array in Class
Why Are Unnamed Namespaces Used and What Are Their Benefits
Accessing Inherited Variable from Templated Parent Class
Using a C++ Class Member Function as a C Callback Function
How to Open an Std::Fstream (Ofstream or Ifstream) With a Unicode Filename