473,320 Members | 2,164 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.

cont char* p , is allowing values to change

Why is gcc allowing the following compilations

#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);
}

It is compiling with a warning only . why is it not giving a compile
time error

Mar 24 '07 #1
7 1721
On Mar 24, 9:12 pm, "parag_p...@hotmail.com" <parag_p...@hotmail.com>
wrote:
Why is gcc allowing the following compilations

#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);

}

It is compiling with a warning only . why is it not giving a compile
time error
it is pointer of const char, not const pointer of char.

Mar 24 '07 #2
pa********@hotmail.com wrote:
>
Why is gcc allowing the following compilations

#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);
}

It is compiling with a warning only .
Only one?
why is it not giving a compile time error
Because the compiler is allowed to do
whatever it wants to do, with code like that.

N869
6.7.3 Type qualifiers

[#5] If an attempt is made to modify an object defined with
a const-qualified type through use of an lvalue with non-
const-qualified type, the behavior is undefined.

--
pete
Mar 24 '07 #3
pete wrote:
>
pa********@hotmail.com wrote:

Why is gcc allowing the following compilations

#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);
}

It is compiling with a warning only .

Only one?
why is it not giving a compile time error

Because the compiler is allowed to do
whatever it wants to do, with code like that.

N869
6.7.3 Type qualifiers

[#5] If an attempt is made to modify an object defined with
a const-qualified type through use of an lvalue with non-
const-qualified type, the behavior is undefined.
I don't think that's the right reason.
*str isn't an lvalue with non-const-qualified type

--
pete
Mar 24 '07 #4
On Mar 24, 1:12 pm, "parag_p...@hotmail.com" <parag_p...@hotmail.com>
wrote:
Why is gcc allowing the following compilations

#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);

}

It is compiling with a warning only . why is it not giving a compile
time error
Perhaps you have an old gcc? For me, it does in fact throw
an error:

[tmp]$ gcc -Wall r.c
r.c:4: warning: return type defaults to `int'
r.c: In function `main':
r.c:7: warning: implicit declaration of function `strcpy'
r.c:7: warning: passing arg 1 of `strcpy' discards qualifiers from
pointer target type
r.c:8: error: assignment of read-only location
[tmp]$ gcc --version
gcc (GCC) 3.4.3 20041212

Mar 24 '07 #5
pete wrote:
>
pete wrote:

pa********@hotmail.com wrote:
>
Why is gcc allowing the following compilations
>
#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);
}
>
It is compiling with a warning only .
Only one?
why is it not giving a compile time error
Because the compiler is allowed to do
whatever it wants to do, with code like that.

N869
6.7.3 Type qualifiers

[#5] If an attempt is made to modify an object defined with
a const-qualified type through use of an lvalue with non-
const-qualified type, the behavior is undefined.

I don't think that's the right reason.
*str isn't an lvalue with non-const-qualified type
OK, here it is.

N869
6.3.2.1 Lvalues and function designators
[#1]...

A modifiable lvalue is an lvalue that does not
have array type, does not have an incomplete type, does not
have a const-qualified type...

6.5.16 Assignment operators
[#2] An assignment operator shall have a modifiable lvalue
as its left operand.
--
pete
Mar 24 '07 #6
pete wrote:
>
pete wrote:

pete wrote:
>
pa********@hotmail.com wrote:

Why is gcc allowing the following compilations

#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);
}

It is compiling with a warning only .
>
Only one?
>
why is it not giving a compile time error
>
Because the compiler is allowed to do
whatever it wants to do, with code like that.
>
N869
6.7.3 Type qualifiers
>
[#5] If an attempt is made to modify an object defined with
a const-qualified type through use of an lvalue with non-
const-qualified type, the behavior is undefined.
I don't think that's the right reason.
*str isn't an lvalue with non-const-qualified type

OK, here it is.

N869
6.3.2.1 Lvalues and function designators
[#1]...

A modifiable lvalue is an lvalue that does not
have array type, does not have an incomplete type, does not
have a const-qualified type...

6.5.16 Assignment operators
[#2] An assignment operator shall have a modifiable lvalue
as its left operand.
The explanation needs these parts too:

N869
3. Terms and definitions
3.18
[#1] undefined behavior
behavior, upon use of a nonportable or erroneous program
construct, of erroneous data, or of indeterminately valued
objects, for which this International Standard imposes no
requirements

4. Conformance
[#1] In this International Standard, ``shall'' is to be
interpreted as a requirement on an implementation or on a
program; conversely, ``shall not'' is to be interpreted as a
prohibition.
[#2] If a ``shall'' or ``shall not'' requirement that
appears outside of a constraint is violated, the behavior is
undefined. Undefined behavior is otherwise indicated in
this International Standard by the words ``undefined
behavior'' or by the omission of any explicit definition of
behavior. There is no difference in emphasis among these
three; they all describe ``behavior that is undefined''.

--
pete
Mar 24 '07 #7
"pa********@hotmail.com" wrote:
>
Why is gcc allowing the following compilations

#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);
}

It is compiling with a warning only . why is it not giving a
compile time error
[1] c:\c\junk>cc junk.c
junk.c:3: warning: return type defaults to `int'
junk.c: In function `main':
junk.c:6: warning: implicit declaration of function `strcpy'
junk.c:6: warning: passing arg 1 of `strcpy' discards qualifiers
from pointer target type
junk.c:7: warning: assignment of read-only location
junk.c:9: warning: control reaches end of non-void function

That isn't enough to give you a hint that some things are wrong?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Mar 24 '07 #8

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

Similar topics

5
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
5
by: gelbeiche | last post by:
Is ( cont.begin() == cont.end() ) essentially equivalent to writing ( cont.empty() ) for a STL container ?
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
20
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
2
by: Robert Fitzpatrick | last post by:
I have a field in my pgsql 7.4.2 table that is char(6) type. This holds an ID that contains only numbers, but must be six characters in length. First two chars are the year followed by a sequential...
33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.