Const char * as a function parameter in C ++

NOTE. I know that there are many questions that talked about this, but I am still a beginner and I could not understand the examples.

I got a function prototype that looks something like this:

int someFunction(const char * sm); 

Here, as you know, const char * means that this function can accept const or non-const pointer-to-char. I tried something like this in the function body:

 someMemberVar = sm; 

someMemberVar is just a pointer to a char. The compiler tells me an error: cannot convert from const char * to char *.

I did not pass a constant here, so sm or someMemberVar are not constants. So what constant is the compiler talking about?

+6
c ++ const
source share
7 answers

I will try to say in simpler words what others say:

The someFunction function accepts a read-only string (for simplicity, although char * could be used in other cases). Regardless of whether you pass the value of someFunction readonly line or not, this parameter is treated as read-only code using code executed in the context of this function. Therefore, inside this function, the compiler will try to prevent you from writing as much as possible to this line. Non-continent pointer - such an attempt to ignore a read-only tag in a string and compiler correctly and loudly informs you of such neglect of your type system;)

What is the difference: int someFunction (const char * sm) const {...} and this: int someFunction (const char * sm) {...}

The first is a function that takes a readonly parameter. The second const , written after the closing parentheses, is valid only for member functions. It not only accepts a read-only parameter, but also ensures that it does not change the state of the object. This is usually called the const design level.

+10
source share

This is not entirely clear from your question, and I suspect that the text of the error you are giving is actually erroneous, and actually reads:

can't convert from const char* to char*

Since you say that

someMemberVar is just a pointer to a char.

It makes sense. Keep in mind that const char* actually matches char const* , that is, it is a pointer to a const char, not a pointer to a const to char! And you cannot convert a pointer to T const to a pointer to T , as this violates type safety. Consider:

 const char* a = "abc"; char* b = a; // what you're trying to do b[0] = 'x'; // if you could do it, you could change const data without casts 
+10
source share

const char* is a pointer to a char constant:

 const char* ptr0; // ptr0 is mutable, *ptr0 is const char* const ptr1; // ptr1 is const, *ptr1 is mutable 
+3
source share

If you pass a non-constant pointer to someFunction , it is automatically converted to a const pointer. Thus, you cannot assign sm to someMemberVar because it violates the sm constant. You can declare someMemberVar as const char* and your code will work, however you cannot change what it points to.

+1
source share

In a comment from one of the other answers, you said:

const char * sm means I can pass const or not const, so why does C ++ convert it automatically? Which doesn't make sense to me.

Between your original question and this comment, I think you don’t understand how the compiler views types.

You are right that non-constant char * can be safely attributed to const char * . However, your method explicitly takes the value const char * . So, although you can pass char * to a function, the compiler just automatically passes the argument when the function is called. The actual code in the function does not know if the original argument was const or not. The compiler should go to the actual declaration of the variable used, and not to the previous variable that has the same value.

If you want to be able to handle non-const char * variables differently (by assigning them), then you will need to define a function that accepts non-const char * variables.

+1
source share

const char * sm - pointer to a constant character (or array). When you try to assign someMemberVar, a pointer to char, you try to point it to a character set constant . This is the cause of the error.

0
source share

In your example, sm is const char *, so someFunction has a contract with it that it will not change the memory pointed to by sm.

But if you assign sm someMemberVar and someMemberVar is not const char *, you can change the memory pointed to by sm through someMemberVar, and the compiler does not allow you to do this.

0
source share

All Articles