473,385 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting an exception..,




#include "stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<string.h>
int main()
{
char *str1 = "United";
char *str2 = "Front";
char *str3;

str3 = strcpy(str1, str2); //AT THIS COMMAND EXCEPTION OCCURS

printf("%s", str3);

getch();

return 0;
}
Not working under MS VS 6.0 and also in Bloodshed Dev C++.
Aug 12 '08 #1
10 1735
On 2008-08-12, Pranav <pr*******@gmail.comwrote:
>
char *str1 = "United";
char *str2 = "Front";
char *str3;

str3 = strcpy(str1, str2); //AT THIS COMMAND EXCEPTION OCCURS
str2 points to a string literal, which is immutable.

Your code has a number of other issues, but this is the cause of
your main complaint.
--
Andrew Poelstra ap*******@wpsoftware.com
To email me, use the above email addresss with .com set to .net
Aug 12 '08 #2
On Tue, 12 Aug 2008 04:34:22 GMT, Andrew Poelstra <no****@nospam.com>
wrote:
>On 2008-08-12, Pranav <pr*******@gmail.comwrote:
>>
char *str1 = "United";
char *str2 = "Front";
char *str3;

str3 = strcpy(str1, str2); //AT THIS COMMAND EXCEPTION OCCURS

str2 points to a string literal, which is immutable.
Of course you meant str1 since that is the target of the copy.
>
Your code has a number of other issues, but this is the cause of
your main complaint.
--
Remove del for email
Aug 12 '08 #3
On 2008-08-12, Barry Schwarz <sc******@dqel.comwrote:
On Tue, 12 Aug 2008 04:34:22 GMT, Andrew Poelstra <no****@nospam.com>
wrote:
>>On 2008-08-12, Pranav <pr*******@gmail.comwrote:
>>>
char *str1 = "United";
char *str2 = "Front";
char *str3;

str3 = strcpy(str1, str2); //AT THIS COMMAND EXCEPTION OCCURS

str2 points to a string literal, which is immutable.

Of course you meant str1 since that is the target of the copy.
Yes, I did. Thank you.

--
Andrew Poelstra ap*******@wpsoftware.com
To email me, use the above email addresss with .com set to .net
Aug 12 '08 #4
Pranav wrote:
>
#include "stdafx.h"
#include<conio.h>
The above do not normally exist in standard C. That makes this an
off-topic post. Just omit those includes. BTW, use a space
between "include" and "<".
#include<stdio.h>
#include<string.h>

int main()
{
char *str1 = "United";
char *str2 = "Front";
These are non-modifiable, i.e. read-only, strings. Use:

char str1[] = "United";

to get an initialized but modifiable string.
char *str3;

str3 = strcpy(str1, str2); //AT THIS COMMAND EXCEPTION OCCURS
and you are trying to modify str1. Not allowed.
>
printf("%s", str3);
getch();
return 0;
}

Not working under MS VS 6.0 and also in Bloodshed Dev C++.
Not of interest on this newsgroup, but explains where you got the
bad habits.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Aug 12 '08 #5
Pranav wrote:
#include "stdafx.h"
^^^^^^^^^^
This is not a standard header.
#include<conio.h>
^^^^^^^^^
This is not a standard header. And a little white space won't kill you.
Hitting the spacebar once will not strain your thumb.
#include<stdio.h>
#include<string.h>
int main()
{
char *str1 = "United";
char *str2 = "Front";
char *str3;
str3 = strcpy(str1, str2); //AT THIS COMMAND EXCEPTION OCCURS
^^^^^^^^^^^^
This is not a command. It is a function call with
its returned value assigned to a variable.
^^^^^^^^^^^^^^^^^^^^
str1 points to a string constant, not to a modifiable
array. If you get a fatal error, that's wonderful. You
have been lucky. And if your implementation calls that an
exception, that's OK, but the word "exception" has no meaning
for C.
printf("%s", str3);
getch();
^^^^^^
This is not a standard function. There are times when non-standard
functionality is necessary, but this isn't one of them: getchar()
would do as well (but a message to the user first is a good idea.
Forcing user to guess what the hell is going on is bad practice).
>
return 0;
}
Not working under MS VS 6.0 and also in Bloodshed Dev C++.
Of course not.
Aug 12 '08 #6
On Aug 12, 11:37 am, Martin Ambuhl <mamb...@earthlink.netwrote:
Pranav wrote:
#include "stdafx.h"

^^^^^^^^^^
This is not a standard header.
#include<conio.h>

^^^^^^^^^
This is not a standard header. And a little white space won't kill you.
Hitting the spacebar once will not strain your thumb.
#include<stdio.h>
#include<string.h>
int main()
{
char *str1 = "United";
char *str2 = "Front";
char *str3;
str3 = strcpy(str1, str2); //AT THIS COMMAND EXCEPTION OCCURS

^^^^^^^^^^^^
This is not a command. It is a function call with
its returned value assigned to a variable.
^^^^^^^^^^^^^^^^^^^^
str1 points to a string constant, not to a modifiable
array. If you get a fatal error, that's wonderful. You
have been lucky. And if your implementation calls that an
exception, that's OK, but the word "exception" has no meaning
for C. printf("%s", str3);
getch();

^^^^^^
This is not a standard function. There are times when non-standard
functionality is necessary, but this isn't one of them: getchar()
would do as well (but a message to the user first is a good idea.
Forcing user to guess what the hell is going on is bad practice).
return 0;
}
Not working under MS VS 6.0 and also in Bloodshed Dev C++.

Of course not.

Thank You All For Your Suggestions and Help. I will take care next
time.
Aug 12 '08 #7
On Aug 12, 1:37*am, Martin Ambuhl <mamb...@earthlink.netwrote:
Pranav wrote:
#include "stdafx.h"

* * * * * * ^^^^^^^^^^
This is not a standard header.
#include<conio.h>

* * * * * *^^^^^^^^^
This is not a standard header. And a little white space won't kill you.
* Hitting the spacebar once will not strain your thumb.
Likewise, letting other people use their own styles won't kill you,
either.
#include<stdio.h>
#include<string.h>
int main()
{
* char *str1 = "United";
* char *str2 = "Front";
* char *str3;
* str3 = strcpy(str1, str2); *//AT THIS COMMAND EXCEPTION OCCURS

* * * * * * * * * * * * * * * * * * * ^^^^^^^^^^^^
* * * * * * * * * * * *This is not a command. It is a function call with
* * * * * * * * * * * *its returned value assigned to a variable.
Actually, it's a statement. Or more specifically, an expression
statement.
* * * * * *^^^^^^^^^^^^^^^^^^^^
* * * * * *str1 points to a string constant, not to a modifiable
* * * * * *array.
It's not a string constant, it's a string literal.
If you get a fatal error, that's wonderful.
You got a weird notion of what's "wonderful."
*You have been lucky.
He got a fatal error... how is that "lucky"?
>* And if your implementation calls that an
* * * * * *exception, that's OK,
It's OK if the compiler gave a warning that contained the word
"exception" (as in the general meaning of "exception"). But if it
"threw" an exception (as in a "program exception"), even at compile
time, then that's not OK.
but the word "exception" has no meaning
* * * * * *for C.
* printf("%s", str3);
* getch();

* * *^^^^^^
* * *This is not a standard function. *There are times when non-standard
* * *functionality is necessary, but this isn't one of them: getchar()
* * *would do as well
No. getchar() and getch() don't do the same thing.
(but a message to the user first is a good idea.
* * *Forcing user to guess what the hell is going on is bad practice).
return 0;
}
Not working under MS VS 6.0 and also in Bloodshed Dev C++.

Of course not.
Sebastian

Aug 12 '08 #8
s0****@gmail.com wrote:
On Aug 12, 1:37*am, Martin Ambuhl <mamb...@earthlink.netwrote:
If you get a fatal error, that's wonderful.

You got a weird notion of what's "wonderful."
It immediately diagnosed the undefined behavior and sent him here for
help. That's wonderful.
*You have been lucky.

He got a fatal error... how is that "lucky"?
Do you think it would have been better to seem to work for a while?
Then later crash after adding a bunch more code? Immediate and
unequivocal responses by the OS to your programming blunders are very
lucky indeed.


Brian

Aug 12 '08 #9
s0s...@gmail.com wrote:
On Aug 12, 1:37�am, Martin Ambuhl <mamb...@earthlink.netwrote:
Pranav wrote:
...
� str3 = strcpy(str1, str2); �//AT THIS COMMAND EXCEPTION OCCURS
� � � � � � � � � � � � � � � � � � � ^^^^^^^^^^^^
� � � � � � � � � � � �This is not a command. It is a function call with
� � � � � � � � � � � �its returned value assigned to a variable.

Actually, it's a statement. Or more specifically, an expression
statement.
You're using "this" to refer to the entire line of code, which is an
expression statement. Martin was using "this" to refer to the function
call expression contained in that expression statement, so his
statement was just as accurate as yours. Since the "exception" almost
certainly occurred while the function call was executing, that
function call was probably also what Pranav was incorrectly
describing as a a command when he used the word "this".

...
If you get a fatal error, that's wonderful.

You got a weird notion of what's "wonderful."
�You have been lucky.

He got a fatal error... how is that "lucky"?
Fatal errors are much better than undetected bugs. They gave him an
opportunity to detect and correct his error before it caused more
serious damage.
� And if your implementation calls that an
� � � � � �exception, that's OK,

It's OK if the compiler gave a warning that contained the word
"exception" (as in the general meaning of "exception"). But if it
"threw" an exception (as in a "program exception"), even at compile
time, then that's not OK.
The behavior of this code is undefined; as far at the standard is
concerned, that would be perfectly acceptable. Anything else is just a
QoI issue.
� getch();
� � �^^^^^^
� � �This is not a standard function. �There are times when non-standard
� � �functionality is necessary, but this isn'tone of them: getchar()
� � �would do as well

No. getchar() and getch() don't do the same thing.
That's what he was referring to when he wrote about "non-standard
functionality". There are things you can do using getch(), that you
can't do using standard C code, but none of those things is necessary
to this function.
Aug 12 '08 #10
jameskuy...@verizon.net wrote:
s0s...@gmail.com wrote:
On Aug 12, 1:37�am, Martin Ambuhl <mamb...@earthlink.netwrote:
Pranav wrote:
...
� str3 = strcpy(str1, str2); �//AT THIS COMMAND EXCEPTION OCCURS
>
� � � � � � �� � � � � � � � � � � � ^^^^^^^^^^^^
� � � � � � �� � � � �This is not a command. Itis a function call with
� � � � � � �� � � � �its returned value assigned to a variable.
Actually, it's a statement. Or more specifically, an expression
statement.

You're using "this" to refer to the entire line of code, which is an
Sorry, that should be "it", not "this".
expression statement. Martin was using "this" to refer to the function
call expression contained in that expression statement, so his
statement was just as accurate as yours. Since the "exception" almost
certainly occurred while the function call was executing, that
function call was probably also what Pranav was incorrectly
describing as a a command when he used the word "this".
Aug 12 '08 #11

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

Similar topics

2
by: darrel | last post by:
I have a question below about an error I am getting: Exception has been thrown by the target of an invocation. Is there anyway to get asp.net to give me more details than the above? My catch...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
1
by: thangchan | last post by:
Hi all, i am getting SQL update problem. as below ======================error messages ======================= Server Error in '/CMS' Application....
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
3
by: Rahul Anand | last post by:
As per our requirements we have a web service which internally connects (Simple HTTP Post Request) to a remote server to initiate some work. We are calling the web service method asynchronously...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
2
by: Scott | last post by:
I'm debugging an xmlrpc client/server application. Often when an exception occurs in the server, I receive only a very short error message on the client. For example: xmlrpclib.Fault: <Fault 1:...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.