Home > front end >  Confusion about const char * declarations in C
Confusion about const char * declarations in C

Time:12-11

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 *.

The example that got me thinking about it is in the answer at Parsing csv with c. In particular, the function defined as:

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. 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. 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?

CodePudding user response:

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)

your comments

My understanding of const char * declarations is that it defines a mutable pointer to an immutable array of characters. 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

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), 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

  • 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. Although it returns char* , this is good practice

  • 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

But having the word const does not change anything about storage, and does not save any space anywhere

  • Related