Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const char * as a function parameter in C++

Tags:

c++

constants

NOTE: I know there are many questions that talked about that but I'm still a beginner and I couldn't understand the examples.

I got a function prototype that goes 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 that in the function body:

someMemberVar = sm;

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

Here, I didn't pass a constant, so either sm or someMemberVar aren't constants. So, what constant the compiler is talking about?

like image 384
Loai Abdelhalim Avatar asked Oct 30 '09 20:10

Loai Abdelhalim


People also ask

What does char * const mean in C?

char* const says that the pointer can point to a char and value of char pointed by this pointer can be changed. But we cannot change the value of pointer as it is now constant and it cannot point to another char.

Can a char * be passed as const * argument?

In general, you can pass a char * into something that expects a const char * without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can't pass a const char * into something expecting a char * (without an explicit cast) because that's ...

What is const function parameter?

Declaring function parameters const indicates that the function promises not to change these values. In C, function arguments are passed by value rather than by reference. Although a function may change the values passed in, these changed values are discarded once the function returns.

What is a char const *?

const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so.


1 Answers

I'll try to put in simpler terms what others are saying:

The function someFunction takes a read-only string (for simplicity's sake, though char * could be used in umpteen other cases). Whether you pass in a readonly string to someFunction or not, the parameter is treated as read-only by the code executing in the context of this function. Within this function therefore, the compiler will try to prevent you from writing to this string as much as possible. A non-const pointer is such an attempt to disregard the read-only tag to the string and the compiler, rightly and loudly informs you of such disregard for its type system ;)

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

The first is a function which takes a readonly parameter. The second const written after the closing parentheses is valid only for member functions. It not only takes a read-only parameter, but also gurantees to not alter the state of the object. This is typically referred to as design level const.

like image 131
dirkgently Avatar answered Oct 01 '22 08:10

dirkgently