473,573 Members | 4,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

wwj
void main()
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
}
Nov 13 '05 #1
28 2756
wwj <ww*******@mail 2.swjtu.edu.cn> scribbled the following:
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";

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Nov 13 '05 #2
In article <87************ *************@p osting.google.c om>, wwj wrote:

Missing inclusion of <stdio.h>.
void main()
int main(void)
{
char* p="Hello";
printf("%s",p);
*p='w';
You're not allowed to change a string literal.
printf("%s",p);
Missing flushing of stdout and "return 0;".
}


Corrected:

#include <stdio.h>

int main(void)
{
char p[] = "Hello";
printf("%s\n", p);
p[0] = 'w';
printf("%s\n", p);
return 0;
}
--
Andreas Kähäri
Nov 13 '05 #3
On 7 Nov 2003 06:37:44 -0800, ww*******@mail2 .swjtu.edu.cn (wwj)
wrote:
void main()
int main(void) !!!!!!
{
char* p="Hello";
char *p = "Hello";
Don't let char* fool you, keep the * on the variable.
printf("%s",p);
printf("%s\n", p);
*p='w';
No! You cannot write to the string literal, "Hello".
printf("%s",p);
Again, you forgot the \n newline char.
return 0;}


Nov 13 '05 #4
Mark A. Odell <no****@embedde dfw.com> scribbled the following:
On 7 Nov 2003 06:37:44 -0800, ww*******@mail2 .swjtu.edu.cn (wwj)
wrote:
{
char* p="Hello";
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.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It was, er, quite bookish."
- Horace Boothroyd
Nov 13 '05 #5
On 7 Nov 2003 14:46:45 GMT, Joona I Palaste <pa*****@cc.hel sinki.fi>
wrote:
{
char* p="Hello";

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.

Nov 13 '05 #6
Mark A. Odell <no****@embedde dfw.com> scribbled the following:
On 7 Nov 2003 14:46:45 GMT, Joona I Palaste <pa*****@cc.hel sinki.fi>
wrote:
{
char* p="Hello";
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.


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.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF
Nov 13 '05 #7
Greetings.

In article <87************ *************@p osting.google.c om>, wwj wrote:
void main()
int main(void)
{
char* p="Hello";
printf("%s",p);
You forgot to #include <stdio.h>.
*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".)
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

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #8
In <87************ *************@p osting.google.c om> ww*******@mail2 .swjtu.edu.cn (wwj) writes:
void main()
Here wrong.
{
char* p="Hello";
printf("%s",p);
Here wrong.
*p='w';
Here wrong.
printf("%s",p);
Here wrong.
}


Congratulations ! With the exception of the braces, you got a single line
of code right, and even that line is not adequate for your purposes.

Do yourself a favour and read a C book.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #9
In <bo**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.fi> writes:
Mark A. Odell <no****@embedde dfw.com> scribbled the following:
On 7 Nov 2003 06:37:44 -0800, ww*******@mail2 .swjtu.edu.cn (wwj)
wrote:
{
char* p="Hello";

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.


It will, once the newbie writes:

char* p, q;

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

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10

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

Similar topics

36
4090
by: Jack Hughes | last post by:
This argument has come up two or three times lately. We have a web standards document at our company that say "Use valid HTML 4; We don't recommend switching to DIV-based CSS-P for layout just yet though, tables are a lot easier." That's because we have a lot of developers who aren't ready to move to CSS-P and DIVs just yet. But I'm...
6
4320
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program)...
3
1387
by: Sukh | last post by:
I have to design a "Online Test Application" and application is going to display question and answers.All the questons are objective type so there is four answer for each question. My Question is where I shuld store the questions and answers. and how application can pick randomly question. right now I have all question in a text file. ...
3
2738
by: MikeY | last post by:
Hi everyone, I'm having problems with my WHERE Clause syntax with in my SQL CommandText. The error that it is display is "You Have No Data". My problem lies with in the WHERE clause not finding up my passed variable from my get/set function. I have done watch's on the variable and the data name I'm looking for is present. I thought my...
7
3370
by: Swinky | last post by:
Mr. Browne's copy code on his web site has saved me. I have been struggling to copy a record with several related sub-form tables. I found code on his web site that copies a sub-form table, implemented it and was successful to make it work. Thank you Mr. Browne! However, the copy code on his web site only updates one sub-form table. My...
1
1055
by: crystalite | last post by:
hi all, I have a problem for logging in. I want user to log in into their system using company GUID and user GUID. But when the logon button is submitted, the GUIDs(which is captured in the database) will be converted back into their various IDs. Don't worry. This is just my testing but i don't know where I've gone wrong because when I tried to...
41
18120
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
13
1641
by: RichardL | last post by:
Hi, I tried creating "left" and "right" links on the top of a page I'll be building. I intended them to be on the opposite sides of the first line. That failed. What am I missing? TIA, Richard
1
4035
by: kfry | last post by:
I am having an issue maybe someone can help with. I have written an application with Access 2003 on my XP pc. I then package it and install it on my VISTA Access 2007 PC. When I do this the wrong version of the application is run. Meaning, the form changes that I just made are not there, they are from the previous version. I know it is the...
0
7707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7799
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8080
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3737
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2226
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.