By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,116 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

cont char* p , is allowing values to change

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.