Placement of the Asterisk in Pointer Declarations

Placement of the asterisk in pointer declarations

4, 5, and 6 are the same thing, only test is a pointer. If you want two pointers, you should use:

int *test, *test2;

Or, even better (to make everything clear):

int* test;
int* test2;

Type Declaration - Pointer Asterisk Position

Stroustrup was asked this and he said (paraphrasing)

  • if you think more C-ish you will say int *a and Employee *pE
    (so in your head you're thinking "the content of a is an integer")
  • if you think more C++-ish you will say int* a and Employee* pE
    (so in your head it's "a is an integer pointer")

You can think however you like, as long as you never declare two
pointers on the same line.

Works for me. I'm an Employee* pE kind of person, but I'm married to an Employee *pE kind of person - my advice would be not to get too worked up about it.

C - initialization of pointers, asterisk position [duplicate]

It does not matter as far as you are declaring only one pointer. It is usually writen like in the second example (in the code I usually read/write) but for the compiler it's the same.

The trouble can come out if you are declaring more than one pointer. For example, this is not declaring two pointer, instead it declares one pointer and one var of type type.

type* var1, var2;

You need to do instead:

type* var1, *var2;

I prefer to use the * by the var always.

C++ style: Stroustrup' s placement of pointer asterisks

C++ emphasis heavily on types and when it comes to pointers declaration, to avoid any sort of confusion, Bjarne suggested - Stick to one pointer per declaration.

From Bjarne Stroustrup's C++ Style and Technique FAQ [emphasis added]:

Is int* p; right or is int *p; right?

Both are "right" in the sense that both are valid C and C++ and both have exactly the same meaning. As far as the language definitions and the compilers are concerned we could just as well say int*p; or int * p;

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.

The critical confusion comes (only) when people try to declare several pointers with a single declaration:

int* p, p1; // probable error: p1 is not an int*

Placing the * closer to the name does not make this kind of error significantly less likely.

int *p, p1; // probable error?

Declaring one name per declaration minimizes the problem - in particular when we initialize the variables. People are far less likely to write:

int* p = &i;
int p1 = p; // error: int initialized by int*

And if they do, the compiler will complain.

Whenever something can be done in two ways, someone will be confused. Whenever something is a matter of taste, discussions can drag on forever. Stick to one pointer per declaration and always initialize variables and the source of confusion disappears.

See The Design and Evolution of C++ for a longer discussion of the C declaration syntax.

Declaring pointers; asterisk on the left or right of the space between the type and name? [duplicate]

It's a matter of preference, and somewhat of a holy war, just like brace style.

The "C++" style

someType* somePtr;

is emphasizing the type of the pointer variable. It is saying, essentially, "the type of somePtr is pointer-to-someType".

The "C" style

someType *somePtr;

is emphasizing the type of the pointed-to data. It is saying, essentially, "the type of data pointed to by somePtr is someType".

They both mean the same thing, but it depends on if a given programmer's mental model when creating a pointer is "focused", so to speak, on the pointed-to data or the pointer variable.

Putting it in the middle (as someType * somePtr) is trying to avoid committing to either one.

Change asterick's position when declaring a pointer

Please have a look at the available options in menu [Tools] -> [Options] -> [Text Editor] -> [C/C++] -> [Formatting] -> [Spacing] -> [Pointer/Reference alignment].

Does the position of an asterisk affect pointers? [duplicate]

No the whitespacing does not have any effect on the pointer declaration, but there is a bug in the C/C++ grammar (well, officially its a feature, but I don't know anyone who likes the behaviour), where this declaration

char* pointer, pointer2;

leaves pointer with the type of char* and pointer2 with the type of char. Because of this, many people prefer to write it down as

char *pointer, *pointer2;
char *pointer, not_pointer;

to make it clearer, that the "pointerness" is part of the name, not type.

What asterisk position means in C functions building?

char* my_function(){...} defines a function returning pointer to char.

char *my_function(){...} defines a function returning pointer to char. Same as above - different style.

char * my_function(){...} defines a function returning pointer to char. Same as above - different style.

What 2 asterisk means? --> a pointer to a pointer.

char **my_function(){...} defines a function returning pointer to pointer to char. Not the same as above - different return type.

char* * **my_function()(){...} defines a function returning pointer to pointer to pointer to pointer to char. Not the same as above - different return type.



Related Topics



Leave a reply



Submit