C ++ Pointer in function

I know that technically all three methods below are valid, but is there a logical reason to do this anyway? I mean, many things in C ++ are "technically sound," but that doesn't make them less stupid.

int* someFunction(int* input)
{
    // code
}  

or

int *someFunction(int *input)
{
    // code
}

or

int * someFunction(int * input)
{
    // code
}

I personally find the third one annoying, but is there a “right” way? I am generally more likely to use the former (since the latter is more like being used as a dereference operator, which it is not)

+3
source share
6 answers

. , . , , . , .

int* someFunction(int* input);, ?

+2

. 1- , 2- ( C).

:

int* p, q; // here p is a pointer to int, but q is just an int!

(int* p) , "int pointer - p", int *p "int - *p" ( ).

+5

, int *p1, p2; , int* p1, p2; int * p1, p2;. , .

, "" "" .

+1

, . *someFunction(someInput) int.

: . ., , .

0

" ". .

The first approach is usually not compatible with having multiple declarators in a single ad, so people who use it usually don't use more than one declarator in an ad

int* p, b; // misleading, since `b` is `int` and not `int*`

However, the first approach has its supporters.

0
source

All are equivalent. I like the third one because it highlights *. Other people are different.

If you are working on a project with others, use the established style. If not, decide your style.

0
source

Source: https://habr.com/ru/post/1735132/


All Articles