what I mean by this question is why is "const" used before argument1 and argument2, couldnt it be just the argument declaration without const? what does const do to the argument? static bool example(const char * argument1, const char * argument2){...}

- 301,070
- 26
- 186
- 335

- 21
- 3
-
1Related: https://stackoverflow.com/q/5503352/2864740 (not the question itself, rather the content) – user2864740 Apr 27 '20 at 23:12
-
It could be made without const btw. – Eraklon Apr 27 '20 at 23:22
-
2This is to ensure the function won't change the char pointed at `argument1`. Also if the function is called with a `const char *` and your function takes a `char *` you'll discard the const qualifier (you'll see a warning at compilation). Also if your function change the value when it's parameter is a string literal you'll have a segmentaion fault. So whenever a function should not modify a parameter it's better to use `const`. – Mickael B. Apr 27 '20 at 23:22
-
@MickaelB. a string literal is an array of non-const char – M.M Apr 27 '20 at 23:23
-
@M.M right my bad i'll edit – Mickael B. Apr 27 '20 at 23:24
2 Answers
A function prototype is a contract between the function and its users
The qualifier const used in the declarations of the function parameters
bool example(const char * argument1, const char * argument2)
gives a guarantee that the pointed objects will not be changed.
Without such a guarantee you may not pass for example to the function a string literal because changing a string literal results in undefined behavior.
So the qualifier const makes the program more clear and safe.

- 301,070
- 26
- 186
- 335
-
It's a non-binding contract , the function might still modify the memory pointed to by the arguments (without causing undefined behaviour in some cases) – M.M Apr 27 '20 at 23:26
-
@M.M You can write any bad code braking a contract. But I think here is nothing to discuss. – Vlad from Moscow Apr 27 '20 at 23:28
-
It's significant whether or not it is UB to break the contract. People often misunderstand the meaning of function prototype , e.g. they think it means the function cannot change the values pointed to, or even that the values pointed to cannot be changed by any means – M.M Apr 27 '20 at 23:30
what I mean by this question is why is "const" used before argument1 and argument2, couldnt it be just the argument declaration without const?
Your example concerns function parameters of type const char *
. It is crucial to understand here that that type designates a pointer to const char
, not a const
pointer to char. That is, the pointer itself may be modified, but it may not be used to modify the data to which it points.
There are several reasons why this is useful, among them:
C permits an argument of type
char *
to be passed to a parameter of typeconst char *
, but not the other way around. Therefore, if the function does not intend to modify the pointed-to data anyway, then declaring the parameter as aconst char *
makes the function applicable to more cases.Declaring the parameter as
const char *
guarantees to callers that even if the argument points to modifiable data (that is, has typechar *
), the function will not, in fact, modify that data. This is an important guarantee to have in many cases.As a special case of the preceding, string literals presented as function arguments decay to pointers of type
char *
, yet undefined behavior occurs if the resulting pointer is used in an attempt to modify the string literal's value. Therefore, it cannot be assumed safe to pass a string literal to a parameter of typechar *
, but it is reasonably safe to pass one to a parameter of typeconst char *
.

- 160,171
- 8
- 81
- 157