Argument types for the strchr() function
Posted: 28 Feb 2006, 17:14
Bryan is trying to compile a Windows executable of Glaurung CCT8, but is having problems with the following function:
Bryan's compiler gives the following error message:
Line 158 is the line with the call to strchr() in the push_button() function. I'm confused by this, because the manual page for strchr() on my machine claims that the first argument is a 'const char *', not a 'char *':
This problem should be easy to fix: Changing the first argument of my push_button() function from a 'const char *' to a 'char *' should work just fine, and does no harm. But which form is correct, according to the C standard? What is the type of the first argument to strchr() supposed to be?
Tord
- Code: Select all
void push_button(const char *button_name) {
uci_option_t *u;
char *c;
// Remove trailing newline character:
c = strchr(button_name, '\n');
if(c != NULL) *c = '\0';
u = option_by_name(button_name);
if(u != NULL) strcpy(u->value, "true");
}
Bryan's compiler gives the following error message:
- Code: Select all
ucioption.cpp(158) : error C2440: '=' : cannot convert from 'const char *'
to 'char *'
Conversion loses qualifiers
Line 158 is the line with the call to strchr() in the push_button() function. I'm confused by this, because the manual page for strchr() on my machine claims that the first argument is a 'const char *', not a 'char *':
- Code: Select all
STRCHR(3) BSD Library Functions Manual STRCHR(3)
NAME
strchr, strrchr -- locate character in string
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <string.h>
char *
strchr(const char *s, int c);
char *
strrchr(const char *s, int c);
This problem should be easy to fix: Changing the first argument of my push_button() function from a 'const char *' to a 'char *' should work just fine, and does no harm. But which form is correct, according to the C standard? What is the type of the first argument to strchr() supposed to be?
Tord