Getting an exception..,

P

Pranav

#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++.
 
A

Andrew Poelstra

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

CBFalconer

Pranav said:
#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.
 
M

Martin Ambuhl

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

Pranav

^^^^^^^^^^
This is not a standard header.


^^^^^^^^^
This is not a standard header. And a little white space won't kill you.
Hitting the spacebar once will not strain your thumb.


^^^^^^^^^^^^
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);

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




Of course not.


Thank You All For Your Suggestions and Help. I will take care next
time.
 
S

s0suk3

            ^^^^^^^^^^
This is not a standard header.


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

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




Of course not.

Sebastian
 
D

Default User

You got a weird notion of what's "wonderful."

It immediately diagnosed the undefined behavior and sent him here for
help. That's wonderful.
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
 
J

jameskuyper

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

...
You got a weird notion of what's "wonderful."


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

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
473,776
Messages
2,569,603
Members
45,198
Latest member
JaimieWan8

Latest Threads

Top