pointer to constant

D

dis_is_eagle

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.But my compiler only shows a warning.Thanks for any help.

Regards,
Eric
 
R

Richard Heathfield

(e-mail address removed) 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.

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.
 
L

lovecreatesbea...

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.But my compiler only shows a warning.Thanks for any help.

The parameter of strcpy corresponding to str1 is not qualified with
const, so the language rules can not prevent it from being modified.
And this is not the point in your code. The warning in your code has
the same meaning in line 9 in the following code. Assigning a non-const
pointer to a const qualified pointer does not mean modifying the later
one. So the compiler gives out warnings on line 8 & 9 but gives an
errer on line 10 in the following example code.

#include <string.h>

int main(void){
char str[] = "hello";
const char *str1 = str;
char *str2;

strcpy(str1, "world"); /*line 8*/
str2 = str1; /*line 9*/
/* *str1 = 'a'; *//*line 10*/

return 0;
}
 
L

lovecreatesbea...

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.But my compiler only shows a warning.Thanks for any help.

The parameter of strcpy corresponding to str1 is not qualified with
const, so the language rules can not prevent it from being modified.
And this is not the point in your code. The warning in your code has
the same meaning in line 9 in the following code. Assigning a non-const
pointer to a const qualified pointer does not mean modifying the later
one. So the compiler gives out warnings on line 8 & 9 but gives an
errer on line 10 in the following example code.

#include <string.h>

int main(void){
char str[] = "hello";
const char *str1 = str;
char *str2;

strcpy(str1, "world"); /*line 8*/

The strcpy call involves undefined behavior of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top