Declaring Pointer Syntax in a Function

Are both pointers the same?

void reverse(const char * const sPtr){ } 

and

 void reverse(const char const *sPtr){ } 
+6
source share
3 answers

Not.

const char const *sPtr equivalent to const char *sPtr .

const char *sPtr say const char *sPtr parameter is a pointer to const char .

const char * const sPtr is a const pointer to const char .

Note that this is equivalent in C99 and C11:

(C99, 6.7.3p4) "If the same qualifier appears more than once in the same list of qualifier-qualifiers, either directly, or through one or more typedefs, the behavior is the same as if it appeared only once."

but not in C89, where const char const *sPtr is a violation of the constraint:

(C90, 6.5.3). The same type of classifier should not appear more than once in the same list of qualifiers or lists of classifiers, either directly, or through one or more typedefs. "

+5
source

What are you trying to achieve?

If you need a const pointer, you must put const after *

 int * const a; 

If you need a pointer to const, you must put const before *

 const int *a; 
+2
source

Not.

The first is a const pointer to const char .

The second is a pointer to const char .

const binds to the left or right - nothing stays left.

+1
source

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


All Articles