为什么strlen的param是const呢?

时间:2022-01-25 19:00:05

I'm learning the C language.

我正在学习C语言。

My question is: Why is the param of strlen a "const" ?

我的问题是:为什么strlen的param是“const”?

size_t strlen(const char * string);

I'm thinking it's because string is an address so it doesn't change after initialization. If this is right, does that mean every time you build a function using a pointer as a param, it should be set to a constant ?

我想是因为字符串是地址,所以初始化后它不会改变。如果这是正确的,这是否意味着每次使用指针作为参数构建函数时,它都应该被设置为常量?

Like if I decide to build a function that sets an int variable to its double, should it be defined as:

例如,如果我决定构建一个将int变量设置为double的函数,是否应该将其定义为:

void timesTwo(const int *num)
{
    *num *= 2;
}

or

void timesTwo(int *num)
{
    *num *= 2;
}

Or does it make no difference at all ?

还是说这没有什么区别?

3 个解决方案

#1


7  

C string is a pointer to a zero-terminated sequence of characters. const in front of char * indicates to the compiler and to the programmer calling the function that strlen is not going to modify the data pointed to by the string pointer.

C字符串是指向零终止字符序列的指针。在char *前面的const向编译器和调用函数的程序员表示,strlen不会修改字符串指针指向的数据。

This point is easier to understand when you look at strcpy:

当你看strcpy时,这一点更容易理解:

char * strcpy ( char * destination, const char * source );

its second argument is const, but its first argument is not. This tells the programmer that the data pointed to by the first pointer may be modified by the function, while the data pointed to by the second pointer will remain constant upon return from strcpy.

它的第二个论点是const,但它的第一个论点不是。这告诉程序员,第一个指针指向的数据可以被函数修改,而第二个指针指向的数据在从strcpy返回时将保持不变。

#2


4  

The parameter to strlen function is a pointer to a const because the function is not expected to change what the pointer points to - it is only expected to do something with the string without altering it.

strlen函数的参数是指向const的指针,因为函数不希望改变指针指向的内容——它只希望在不改变字符串的情况下处理字符串。

In your function 'timesTwo', if you intend to change the value that 'num' points to, you should not pass it in as a pointer-to-const. So, use the second example.

在函数“timesTwo”中,如果您打算更改“num”指向的值,则不应该将其作为pointerto -const传递。用第二个例子。

#3


4  

Basically, the function is promising that it will not modify the contents of of the input string through that pointer; it says that the expression *string may not be written to.

基本上,该函数保证它不会通过该指针修改输入字符串的内容;它说表达式*string可能不会被写入。

Here are the various permutations of the const qualifier:

下面是const限定符的各种排列:

const char *s;            // s may be modified, *s may not be modified
char const *s;            // same as above
char * const s;           // s may not be modified, *s may be modified
const char * const s;     // neither s nor *s may be modified
char const * const s;     // same as above

#1


7  

C string is a pointer to a zero-terminated sequence of characters. const in front of char * indicates to the compiler and to the programmer calling the function that strlen is not going to modify the data pointed to by the string pointer.

C字符串是指向零终止字符序列的指针。在char *前面的const向编译器和调用函数的程序员表示,strlen不会修改字符串指针指向的数据。

This point is easier to understand when you look at strcpy:

当你看strcpy时,这一点更容易理解:

char * strcpy ( char * destination, const char * source );

its second argument is const, but its first argument is not. This tells the programmer that the data pointed to by the first pointer may be modified by the function, while the data pointed to by the second pointer will remain constant upon return from strcpy.

它的第二个论点是const,但它的第一个论点不是。这告诉程序员,第一个指针指向的数据可以被函数修改,而第二个指针指向的数据在从strcpy返回时将保持不变。

#2


4  

The parameter to strlen function is a pointer to a const because the function is not expected to change what the pointer points to - it is only expected to do something with the string without altering it.

strlen函数的参数是指向const的指针,因为函数不希望改变指针指向的内容——它只希望在不改变字符串的情况下处理字符串。

In your function 'timesTwo', if you intend to change the value that 'num' points to, you should not pass it in as a pointer-to-const. So, use the second example.

在函数“timesTwo”中,如果您打算更改“num”指向的值,则不应该将其作为pointerto -const传递。用第二个例子。

#3


4  

Basically, the function is promising that it will not modify the contents of of the input string through that pointer; it says that the expression *string may not be written to.

基本上,该函数保证它不会通过该指针修改输入字符串的内容;它说表达式*string可能不会被写入。

Here are the various permutations of the const qualifier:

下面是const限定符的各种排列:

const char *s;            // s may be modified, *s may not be modified
char const *s;            // same as above
char * const s;           // s may not be modified, *s may be modified
const char * const s;     // neither s nor *s may be modified
char const * const s;     // same as above