Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signature of std::strtol

Tags:

c++

The signature of std::strtol is formulated as follows:

long strtol(const char *str, char **str_end, int base);

I actually expect the second parameter to be of type const char**. Any explanation on why it is of type char** instead? Note that, it is possible to modify a variable of type const char**. What you cannot modify is a char* const * variable.

like image 522
Lingxi Avatar asked Dec 17 '15 06:12

Lingxi


1 Answers

In the days of yore (before C90) C had no concept of const and everything used plain char*.

Since char* can be implicitly converted to a char const* changing the signature of most library functions to support const wasn't too much of an issue. char** however can't be converted to a char const**, see the following note from the C++ standard for why

enter image description here

Since C doesn't support overloading the committee had to choose between breaking legacy code, or forcing everyone to pass in a char**. Neither is ideal but it looks like they went with the latter.

like image 200
user657267 Avatar answered Oct 03 '22 06:10

user657267