Exforsys

Online Training

Program Crashing

This is a discussion on Program Crashing within the C and C++ forums, part of the Programming Talk category; I have written my C code as below: #include<stdio.h> main() { char *e = "WELCOME"; char *...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-23-2007, 12:10 AM
Senior Member
 
Join Date: Apr 2006
Posts: 122
rachelle is on a distinguished road
Question Program Crashing

I have written my C code as below:
#include<stdio.h>
main()
{

char *e = "WELCOME";
char *f,*g;
f = e;
g = e+strlen(e)-1;
printf("%c, %c",*f,*g);
*f=*g;
printf("%c, %c",*f,*g);
}

What I am trying out is trying to first print out the first and the last letter of the given string and then assign the last letter to the first and print the first and last letter with the same letter as last letter. That is first to print W and E and then to print E E. But when I tried the above program it is crashing and is giving me segmentation fault. I could not figure out the reason. Can someone help me out?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-23-2007, 01:36 AM
Junior Member
 
Join Date: May 2007
Posts: 9
hvg_99 is on a distinguished road
Program Crashing: Answer

#include<stdio.h>
main()
{

char *e = "WELCOME";
char *f,*g;
f = e;
g = e+strlen(e)-1;
printf("%c, %c",*f,*g);
*f=*g;
printf("%c, %c",*f,*g);
}

Hi,

The above program crashes because:

till the first printf everything is fine. but later you are trying to put the address contained in g to f. and since this cannot be done and hence the error.

I hope i have cleared your doubt.

Regards,
Gayatri
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-24-2007, 04:34 PM
Senior Member
 
Join Date: Apr 2006
Posts: 125
ashlee is on a distinguished road
Yes as our friend has suggested till the first printf all is good. The program if you debug you would find it crashing at the second printf only. This is because you are trying to access and write to a protected memory space. This type of protected memory space can be used for reading but when you try to write into this you would face such error. To correct the above program just change as
char e[] = "WELCOME";
char *f = e;
...........
...........
and proceed with the other code and your code should work fine.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-15-2004 07:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-01-2004 07:01 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 06-01-2004 07:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 05-15-2004 07:00 AM
Apple II Csa2 FAQs: Telecom Hardware & Transfers, Part 20/25 rubywand@swbell.net Tech FAQ 0 04-04-2004 08:29 AM


All times are GMT -4. The time now is 12:22 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.