cont char* p , is allowing values to change

P

parag_paul

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
 
H

Homuncilus

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

pete

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

pete

pete said:
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
 
B

Bill Pursell

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
 
P

pete

pete said:
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.
 
P

pete

pete said:
(e-mail address removed) 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''.
 
C

CBFalconer

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?
 

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

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top