简体   繁体   English

关于 C 中 const char * 声明的混淆

[英]Confusion about const char * declarations in C

I know this may seem like a stupid question, but I am really having trouble understanding some particular examples of C code that involve a declaration of type const char * .我知道这可能看起来像一个愚蠢的问题,但我真的很难理解 C 代码的一些特定示例,这些示例涉及const char *类型的声明。

The example that got me thinking about it is in the answer at Parsing csv with c .让我想到它的例子是在Parsing csv with c的答案中。 In particular, the function defined as:特别是,function 定义为:

const char* getfield(char* line, int num)
{
    const char* tok;
    for (tok = strtok(line, ";");
            tok && *tok;
            tok = strtok(NULL, ";\n"))
    {
        if (!--num)
            return tok;
    }
    return NULL;
}

My understanding of const char * declarations is that it defines a mutable pointer to an immutable array of characters.我对const char *声明的理解是它定义了一个指向不可变字符数组的可变指针。 This array is assigned in the data section at compile time.该数组在编译时分配在数据段中。 However in this case (and many other code examples), the const char * declaration is not for a string literal.但是在这种情况下(以及许多其他代码示例), const char *声明不适用于字符串文字。 How can this memory be assigned for this at compile time if the actual value of the string is not known until the function executes?如果在 function 执行之前不知道字符串的实际值,那么如何在编译时为此分配 memory?

this这个

const char* tok;

creates a pointer on the stack.在堆栈上创建一个指针。 It has an undefined value, it points nowhere它有一个未定义的值,它指向任何地方

this这个

for (tok = strtok(line, ";");

then makes it point to somewhere in line (depends on contents)然后让它指向line中的某个地方(取决于内容)

your comments你的评论

My understanding of const char * declarations is that it defines a mutable pointer to an immutable array of characters.我对 const char * 声明的理解是它定义了一个指向不可变字符数组的可变指针。 This array is assigned in the data section at compile time.该数组在编译时分配在数据段中。

First sentence, almost, it creates a pointer that might point to an immutable array of chars.第一句话,几乎,它创建了一个指针,该指针可能指向一个不可变的字符数组。 Like this像这样

  const char * msg = "hello world";

But is can also point nowhere但也无处可去

 const char * msg = NULL;

Or to a mutable array of characters或者一个可变的字符数组

 char msg[] = "HelloWorld"; // mutable
 const char *msgconst = msg;

the second sentence第二句

This array is assigned in the data section at compile time这个数组在编译时赋值在数据段

Yes, if this is a literal.是的,如果这是一个文字。 Ie like this example again我又喜欢这个例子

  const char*msg = "Hello World";

but the msgconst assignment is not like that, the characters are on the stack但是msgconst赋值不是这样的,字符在栈上

Hope this helps希望这可以帮助

EDIT编辑

The pointer points where it is told to do, making it const or not has zero effect on where the data is.指针指向它被告知要做的事情,使其成为常量或不常量对数据的位置影响为零。 The data is where it is.(.), You need to think about where the data is created, nothing to do with the pointer, the pointer can point at a literal, stack data, static data (mutable in data segment).数据就在哪儿。(.),你要考虑数据是从哪里创建的,跟指针无关,指针可以指向一个字面量,栈数据,static数据(数据段可变)。 heap.堆。

Why use const为什么要使用常量

  • if the string is immutable then the pointer must be const.如果字符串是不可变的,那么指针必须是常量。 C allows you to assign a literal to a non const pointer for historical reasons, c++ does not由于历史原因,C 允许您将文字分配给非常量指针,c++ 则不允许

  • You want to make it clear that this string should not be changed.您要明确表示不应更改此字符串。 In your code case you really should not mess with the string strtok is processing.在您的代码案例中,您真的不应该弄乱 strtok 正在处理的字符串。 Although it returns char*, this is good practice虽然它返回 char*,但这是一个很好的做法

  • functions like strlen that do not change the string passed to them declare their arguments as const char*, its part of their contract: we dont change the string像 strlen 这样不改变传递给它们的字符串的函数将它们的 arguments 声明为 const char*,这是它们合同的一部分:我们不改变字符串

But having the word const does not change anything about storage, and does not save any space anywhere但是使用const这个词不会改变存储的任何内容,也不会在任何地方节省任何空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM