Pointer arithmetic and constant determinant in C

The following code snippet for calculating strlen

int s(const char* str)
{   
    int count=0;        
    while(*str++) count++;
    return count;
}

You can see that the str argument is a constant. But the compiler does not complain when I do str ++. My question is:

When passing pointers as arguments to C, if is matches const, how can I do pointer arithmetic on it? What is const in the above function?

+3
source share
4 answers
const char* str;

means a non-constant pointer to const data.

char* const str;

means a constant pointer to non-constant data.

const char* const str;

means const-pointer to const data.

, ++ , , "const" , .

+10

const, , const. :

int s(const char* const str)

str.

+5
const char * ch; // non-const pointer to const data
char * const ch; // const pointer to non-constant data.

const char * const ch; // const pointer to const data

Note: Also

const char * ch;

equally

char  const * ch;
+2
source

A pointer indicates a const charread-only array of characters.

0
source

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


All Articles