Program Crash

K

Kuku

My Program is crashing at
strupr(e2.n);




int main()
{
struct emp
{
char *n;
int age;
};

struct emp e1={"Dravid",50};
struct emp e2;
e2=e1;
strupr(e2.n);
printf("\ne1.n=%s\n",e1.n);
return 0;
}
 
C

Chris McDonald

Kuku said:
My Program is crashing at
strupr(e2.n);

Your question is off topic in comp.lang.c.
The Standard C language does not define what a crashing program is.
 
S

Serge Paccalin

Le mercredi 3 août 2005 à 11:30, Kuku a écrit dans comp.lang.c :
My Program is crashing at
strupr(e2.n);

I guess you want ask us why?
int main()
{
struct emp
{
char *n;
int age;
};

struct emp e1={"Dravid",50};

The constant litteral "Dravid" is stored somewhere in a place of memory
you're not supposed to modify, and e1.n now points to that memory.
struct emp e2;
e2=e1;

Now e2.n points to the same memory as e1.n
strupr(e2.n);

Here, you try to write in that memory. Since you're not supposed to
modify it, anything can happen. That is undefined behavior.

--
___________ 03/08/2005 12:09:13
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
D

Default User

Chris McDonald wrote:

Your question is off topic in comp.lang.c.
The Standard C language does not define what a crashing program is.

Don't be an idiot.




Brian
 
E

Emmanuel Delahaye

Kuku wrote on 03/08/05 :
My Program is crashing at
strupr(e2.n);

int main()
{
struct emp
{
char *n;
int age;
};

struct emp e1={"Dravid",50};
struct emp e2;
e2=e1;
strupr(e2.n);
printf("\ne1.n=%s\n",e1.n);
return 0;
}

A string literal is not mutable. The copy of structures creates a new
pointer to the same not mutable string.

You want an array of char.

#include <string.h>
#include <stdio.h>

struct emp
{
char n[32];
int age;
};

int main ()
{

struct emp e1 =
{
.n = "David",
.age = 50,
};
struct emp e2 = e1;

strupr (e2.n);

printf ("e1.n=%s\n", e1.n);
printf ("e2.n=%s\n", e2.n);

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
K

Keith Thompson

Serge Paccalin said:
Le mercredi 3 août 2005 à 11:30, Kuku a écrit dans comp.lang.c : [...]
strupr(e2.n);

Here, you try to write in that memory. Since you're not supposed to
modify it, anything can happen. That is undefined behavior.

Assuming that strupr modifies the string to which e2.n points. That
seems likely, but there is no strupr() function in the standard.

Also, the original program is missing a #include directive for
<stdio.h> (it calls printf) and for whatever header declares strupr().
 

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

Similar Threads


Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top