di**********@yahoo.com said:
Hi.I have a doubt about the following program.
int main()
{
char str[]="hello";
const char* str1=str;
strcpy(str1,"world");
return 0;
}
I thought as str1 is constant, so copying world to it will give an
error.
Since there is no prototype in scope for strcpy, the compiler is under no
obligation to "know" what kinds of parameters it expects. It is certainly
under no obligation to "know" that strcpy writes through the pointer you
give it as its first parameter.
If you #include <string.hso that strcpy's prototype is provided, the
compiler would then be *required* to issue a diagnostic message.
But my compiler only shows a warning.
The compiler issued a diagnostic message. Just because it says "warning"
rather than "error", that doesn't mean the code is fine. On the contrary,
it means the compiler is worried. Maybe it's not worried enough to stop the
compilation, but it's still fairly concerned, and so should you be.
Don't think of warnings as "only a warning". Think of them as "uh-oh, I
probably messed up big time - I'd better check the code and see what I did
wrong." Modern compilers tend to give very good diagnostic messages.
Sometimes, they do issue what we might consider to be "spurious" messages,
but that doesn't happen very often. Unless you are **absolutely sure** that
you know **exactly** why a warning message is spurious, treat it as an
error, and fix your code accordingly.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)