Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way of declaring pointer variables in C/C++ [closed]

I noticed some people use the following notation for declaring pointer variables.

(a) char* p; 

instead of

(b) char *p; 

I use (b). What is the rational behind the notation (a)? Notation (b) makes more sense to me because character pointer is not a type itself. Instead the type is character and the variable may be a pointer to the character.

char* c; 

This looks like there is a type char* and the variable c is of that type. But in fact the type is char and *c (the memory location pointed by c) is of that type (char). If you declare multiple variables at once this distinction becomes obvious.

char* c, *d; 

This looks weird. Both c and d are same kind of pointers that point to a character. In this since the next one looks more natural.

char *c, *d; 
like image 307
keeda Avatar asked Aug 09 '11 02:08

keeda


People also ask

What is the correct way of declaring pointer variable?

The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double) too. Naming Convention of Pointers: Include a "p" or "ptr" as prefix or suffix, e.g., iPtr, numberPtr, pNumber, pStudent.

How do you declare a pointer variable in C?

For example, if you want a pointer to point to a variable of data type int, i.e. int var=5 then the pointer must also be of datatype 'int', i.e. int *ptr1=&var. The * symbol indicates that the variable is a pointer. To declare a variable as a pointer, you must prefix it with *.

What is the correct syntax of declaring a pointer in C?

Which is correct syntax for declaring pointer to object? Explanation: The syntax must contain * symbol after the className as the type of object. This declares an object pointer. This can store address of any object of the specified class.

Which is the legal way to declare pointer in C for String?

Declaring a Pointer in C The datatype of the pointer and the variable to which the pointer variable is pointing must be the same. Just like a variable, pointer is declared in the same way, just with an additional pointer operator * .


1 Answers

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.

like image 187
BlackJack Avatar answered Oct 26 '22 23:10

BlackJack