473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

pointer to constant

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

Sep 5 '06 #1
3 1981
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)
Sep 5 '06 #2

di**********@yahoo.com wrote:
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;
}

Sep 5 '06 #3

lovecreatesbea...@gmail.com wrote:
di**********@yahoo.com wrote:
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.
str2 = str1; /*line 9*/
/* *str1 = 'a'; *//*line 10*/

return 0;
}
Sep 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
69
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
22
by: nick | last post by:
i do not know what is the use of (e.g. void *pt), when will use it. thanks!
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
17
by: matevzb | last post by:
I've ran into some fishy code that, at first glance, is buggy, but it seems to work correctly and none of the compilers I've tried (five so far, on various systems) gives any warnings. The code:...
17
by: Pietro Cerutti | last post by:
i Group, to my understanding, defining a function parameter as "const" means that the function is not going to change it. Why does the compiler says "return discards qualifiers from pointer...
41
by: simonl | last post by:
Hi, I've been given the job of sorting out a crash in our product for which we have the crash information and an avi of the event (which can't possibly match but more of that later...) (btw this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.