W
wwj
void main()
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
}
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
}
wwj said:void main()
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
}
wwj wrote: said:void main()
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
void main()
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
char *p = "Hello";
Don't let char* fool you, keep the * on the variable.
Note to newbies: This is an entirely stylistic correction and will
not affect the produced program at all.
Unless you write:
char* pFoo, pBar, pBaz;
and expect to get three pointers to char.
void main()
{
char* p="Hello";
printf("%s",p);
You forgot to #include said:*p='w';
printf("%s",p);
In said:Note to newbies: This is an entirely stylistic correction and will
not affect the produced program at all.
In said:void main()
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
wwj said:On Fri, 07 Nov 2003 15:23:03 GMT, "Dan Pop" said:
Do you actually contribute anything here, or do you just repost advice that
others have already given?
Do you actually contribute anything here, or do you just repost advice that
others have already given?
On Fri, 07 Nov 2003 15:23:03 GMT, "Dan Pop" said:
Do you actually contribute anything here, or do you just repost advice that
others have already given?
Do you actually contribute anything here, or do you just repost
advice that others have already given?
one of the
more knowledgeable participants, who in turn is not noted for his
gentle handling of fools, nor for his diplomatic skills.
Tristan Miller said:Greetings.
void main()
int main(void)
{
char* p="Hello";
printf("%s",p);
You forgot to #include said:*p='w';
You are not premitted to overwrite a string literal, which is what p points
to. You could solve this problem by making p and array and initializing it
with the string "hello":
char p[6] = "Hello";
(The array length is 6 because C requires strings to contain a special
end-of-string marker, or "null character".)
Since this is the last output of the program, it needs to be followed by a
newline. Otherwise you invoke undefined behaviour.
nobody said:Tristan Miller said:Greetings.
Undefined?
N869
7.19.2 Streams
[#2]
Whether the last line requires
a terminating new-line character
is implementation-defined.
pete said:nobody said:So ... does it mean that absence of '\n' invokes undefinedTristan Miller said:Greetings.
Undefined?
N869
7.19.2 Streams
[#2]
Whether the last line requires
a terminating new-line character
is implementation-defined.
behaviour? Or it's presence? From your citation then, it would
seem that whatever you do, you can't have truly portable program,
if you are sending "data" to stdout/stderr. But I must be wrong,
so what did I miss? Why was my citation irrelevant to the case?
nobody said:pete said:So ... does it mean that absence of '\n' invokes undefinednobody said:Greetings.
printf("%s",p);
Since this is the last output of the program,
it needs to be followed by a newline.
Otherwise you invoke undefined behaviour.
Undefined?
N869
7.19.2 Streams
[#2]
Whether the last line requires
a terminating new-line character
is implementation-defined.
behaviour?
It means that if the stream is not terminated with a newline,
that the code is not portable.
On a system which defines a stream as ending in a newline,
if you have something that's just like a stream,
except for the way that it's terminated, then it's not a stream,
and it isn't defined.
Or it's presence? From your citation then, it would
seem that whatever you do, you can't have truly portable program,
if you are sending "data" to stdout/stderr.
All you have to do, is make sure that the last output character
to the standard output stream, is a null character, to be portable.
But I must be wrong,
so what did I miss? Why was my citation irrelevant to the case?
Because it has nothing to do with whether or not the last output
character in a text stream should be a null character.
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.