P
Paul N
Consider the following bit of code:
int find(char * & in, char ch) {
for ( ; *in; in++) if (*in == ch) return 1;
return 0; }
void replaceonce(char *in, char from, char to) {
if (find(in, from)) *in = to; }
It compiles fine and seems to work OK.
However, since "find" doesn't alter the string, it might be a good
idea to use a "const". But if I change it to:
int find(const char * & in, char ch) {
for ( ; *in; in++) if (*in == ch) return 1;
return 0; }
void replaceonce(char *in, char from, char to) {
if (find(in, from)) *in = to; }
then this doesn't work. VC++ won't compile it at all, saying:
error C2664: 'find' : cannot convert parameter 1 from 'char *' to
'const char *&'
Turbo C++ will compile it, with a warning:
Warning C:\CPP\REFTEST.CPP 9: Temporary used for parameter 'in' in
call to 'find(const char far* far&,char)' in function replaceonce(char
far*,char,char)
and sure enough, it modifies something other than "in".
What I'm trying to do seems reasonable - is there something obvious
that I'm missing?
Thanks.
Paul.
int find(char * & in, char ch) {
for ( ; *in; in++) if (*in == ch) return 1;
return 0; }
void replaceonce(char *in, char from, char to) {
if (find(in, from)) *in = to; }
It compiles fine and seems to work OK.
However, since "find" doesn't alter the string, it might be a good
idea to use a "const". But if I change it to:
int find(const char * & in, char ch) {
for ( ; *in; in++) if (*in == ch) return 1;
return 0; }
void replaceonce(char *in, char from, char to) {
if (find(in, from)) *in = to; }
then this doesn't work. VC++ won't compile it at all, saying:
error C2664: 'find' : cannot convert parameter 1 from 'char *' to
'const char *&'
Turbo C++ will compile it, with a warning:
Warning C:\CPP\REFTEST.CPP 9: Temporary used for parameter 'in' in
call to 'find(const char far* far&,char)' in function replaceonce(char
far*,char,char)
and sure enough, it modifies something other than "in".
What I'm trying to do seems reasonable - is there something obvious
that I'm missing?
Thanks.
Paul.