where wrong?how to correct?and why? thank you

J

Joona I Palaste

wwj said:
void main()

This is a non-standard form of main(). There is no guarantee it will
work. Better use: int main(void)
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
}

The assignment *p='w' modifies a string literal, causing undefined
behaviour. On most implementations, string literals reside in read-only
memory. Thus *p='w' can cause a segfault or something similar.
Try instead: char p[6]="Hello";
 
J

Joona I Palaste

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

Mark A. Odell

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

Joona I Palaste

Unless you write:
char* pFoo, pBar, pBaz;
and expect to get three pointers to char.

That is true, but what I was talking about was strictly what you wrote
in "correction" of the OP's code. The definition above is equivalent
to:

char *pFoo, pBar, pBaz;

or to:
char
* pFoo
, pBar ,
pBaz ;

What matters is how many * there are and between which words they
occur. Whether they "bind" to the type words or the variable name
words is irrelevant.
 
T

Tristan Miller

Greetings.

void main()

int main(void)
{
char* p="Hello";
printf("%s",p);

You forgot to #include said:

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".)
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. Try:

printf("%s\n", p);

You have ended main without returning a value. The preferred method for an
exit without errors is:

return EXIT_SUCCESS;

The EXIT_SUCCESS macro is #defined in the <stdlib.h> header, which you must
#include before you use it. Alternatively, you could just write

return 0;

Regards,
Tristan
 
D

Dan Pop

In said:
Note to newbies: This is an entirely stylistic correction and will
not affect the produced program at all.

It will, once the newbie writes:

char* p, q;

and expects the declaration to have the "obvious" meaning.

Dan
 
W

wwj

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?
 
J

Joona I Palaste

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?

You're new here, aren't you? Dan Pop is one of the most clueful people
here. He is not always polite but he is nearly always right. He has
answered a great deal of questions here, even more than I have. Just
because he reposts *one* piece of advice that's already been given
doesn't mean he never contributes anything.
 
I

Irrwahn Grausewitz

[top-posting fixed; please don't top-post; thank you]
Do you actually contribute anything here, or do you just repost advice that
others have already given?

I'd like to ask you some questions:

Do you actually now how usenet works? Do you now what off-line reading
means? Did you ever notice that propagation of messages is a non-linear
process? What is wrong with two people making a similar remark,
especially if it's correct? Did you ever read any more messages by Dan
Pop than the one you just responded to?

Regards
 
S

Sheldon Simms

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?

No, I don't think "Dan Pop" actually wrote that.
Please attribute correctly and don't top post. Thanks.
 
C

CBFalconer

*** Rude top-posting corrected ***
Do you actually contribute anything here, or do you just repost
advice that others have already given?

Congratulations. You have just rudely contravened standard
practice here by top-posting, and gratuitously insulted one of the
more knowledgeable participants, who in turn is not noted for his
gentle handling of fools, nor for his diplomatic skills. You
contrived to do all this in only two lines. This is a well tested
method of attracting future help.

In the likely case that you are too mentally challenged to
recognize it, you have just been flamed and insulted. A
productive response would be abject apology and grovelling.
 
T

Tak-Shing Chan

On Fri, 7 Nov 2003, CBFalconer wrote:

[Talking about Dan Pop.]
one of the
more knowledgeable participants, who in turn is not noted for his
gentle handling of fools, nor for his diplomatic skills.

I think his diplomatic skills have improved significantly
during the past two years. :)

Tak-Shing, biased sampling
 
N

nobody

Tristan Miller said:
Greetings.

void main()

int main(void)
{
char* p="Hello";
printf("%s",p);

You forgot to #include said:

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".)

That's all true, but why to count (and eventually not re-count)
if compiler does the grunt work? Right, Joona?

char p[] = "Hello";
Since this is the last output of the program, it needs to be followed by a
newline. Otherwise you invoke undefined behaviour.

Undefined? It's not even unintended. *Because* it is
"last output of the program".
[N869]
7.19.3 Files
....
5 ... If the main function returns to its original caller,
or if the exit function is called, all open files are
closed (hence all output streams are flushed) before
program termination. Other paths to program termination,
such as calling the abort function, need not close all
files properly.

[snip]
 
N

nobody

pete said:
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.
So ... does it mean that absence of '\n' invokes undefined
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?
 
P

pete

nobody said:
pete said:
nobody 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.
So ... does it mean that absence of '\n' invokes undefined
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.
 

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,099
Messages
2,570,626
Members
47,237
Latest member
David123

Latest Threads

Top