Evan Hillas wrote:
Evan Hillas wrote:
parse.C: In function `bool parse_int(const char*, const char*, int&)':
parse.C:68: error: invalid conversion from `const char*' to `char*'
Lines 67 and 68 from parse.C ...
bool parse_int(const char* buf, const char* tag, int& x) {
char* p = strstr(buf, tag);
Oh, click!, it is returning a pointer to a const char which means p
should be declared as such. Hmm, I just assumed they would have
something like that all nice and clean in such, supposedly, portable code.
Yes, you should declare p as "const char* p".
But unless you're compiling in C++ mode, the definition of "strstr"
should be
char *strstr(const char *s1, const char *s2);
This is a constness violation, but standardized as such in IEEE Std 1003.1-2001
for backwards compatibility with code that predates the
addition of "const" to C.
In C++ mode, there are two forms of "strstr", const and non-const.
Only C++ supports such overloading. So in C++, if you
put in "const" arguments to strstr, you get a const output,
which looks like what you're seeing.
So if you're getting that error, I suspect you've somehow
enabled C++ mode in "gcc". Which isn't necessarily bad;
good C code should compile in C++ mode.
Or maybe the Gnu people decided to pull that old compatibility hack
out.
John Nagle