Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Segmentation Fault (core dumped)

Reply
Thread Tools

Segmentation Fault (core dumped)

 
 
DanielJohnson
Guest
Posts: n/a
 
      02-18-2007
I wrote this small program to reverse each word in the string. For
example: "I love You" should print as "I evoL uoY". I get Segmentation
Fault (core dumped) error upon running the program. It compiles fine.

// Program to reverse each word in the string
#include<stdio.h>

int main()
{
void reverse_string(char *, int, int);
char *p = "my name is daniel";
// keeps count of number is char in the word to reverse
int count = 0;
// keeps track of where I started this current word from
int current_pos = 0;
// Conitnue till you reach \0
while(1){
if (*p == '\0') break;
count = 0;
while(*p != ' '){
if (*p == '\0') break;
count++;
p++;
}
reverse_string(p, current_pos, count);
if (*p == ' '){
p++;
count++;
current_pos = count;
}
}
puts(p);
return 0;
}

void reverse_string(char* s, int start, int count){
int counter = count/2;
int i = 0;
while(i < counter){
*s = *(s-count);
s--;
i++;
}
}


Any suggestions......Every help is appreciated.

Thanks

 
Reply With Quote
 
 
 
 
Zack
Guest
Posts: n/a
 
      02-18-2007
DanielJohnson wrote:

> // Program to reverse each word in the string
> #include<stdio.h>
>
> int main()
> {
> void reverse_string(char *, int, int);
> char *p = "my name is daniel";
> // keeps count of number is char in the word to reverse
> int count = 0;
> // keeps track of where I started this current word from
> int current_pos = 0;
> // Conitnue till you reach \0
> while(1){
> if (*p == '\0') break;


You correctly note that you want to continue until reaching the null
character in the string.

However you do not have a null character in your string.
Try :
char *p = "my name is daniel\0";

--
Zack
 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      02-18-2007
>I wrote this small program to reverse each word in the string. For
>example: "I love You" should print as "I evoL uoY". I get Segmentation
>Fault (core dumped) error upon running the program. It compiles fine.


You stored the test string in a quoted string literal. C is permitted
to store such data in a read-only section of the program. If you
try writing on it, KABOOM!

Note also that you keep incrementing p, losing track of where the
beginning of the string is. That's not good when you try to print it.

>
>// Program to reverse each word in the string
>#include<stdio.h>
>
>int main()
>{
> void reverse_string(char *, int, int);
> char *p = "my name is daniel";
> // keeps count of number is char in the word to reverse
> int count = 0;
> // keeps track of where I started this current word from
> int current_pos = 0;
> // Conitnue till you reach \0
> while(1){
> if (*p == '\0') break;
> count = 0;
> while(*p != ' '){
> if (*p == '\0') break;
> count++;
> p++;
> }
> reverse_string(p, current_pos, count);
> if (*p == ' '){
> p++;
> count++;
> current_pos = count;
> }
> }
> puts(p);
> return 0;
>}
>
>void reverse_string(char* s, int start, int count){
> int counter = count/2;
> int i = 0;
> while(i < counter){
> *s = *(s-count);
> s--;
> i++;
> }
>}
>
>
>Any suggestions......Every help is appreciated.
>
>Thanks
>



 
Reply With Quote
 
DanielJohnson
Guest
Posts: n/a
 
      02-18-2007
On Feb 18, 12:29 am, Zack <(E-Mail Removed)> wrote:
> DanielJohnson wrote:
> > // Program to reverse each word in the string
> > #include<stdio.h>

>
> > int main()
> > {
> > void reverse_string(char *, int, int);
> > char *p = "my name is daniel";
> > // keeps count of number is char in the word to reverse
> > int count = 0;
> > // keeps track of where I started this current word from
> > int current_pos = 0;
> > // Conitnue till you reach \0
> > while(1){
> > if (*p == '\0') break;

>
> You correctly note that you want to continue until reaching the null
> character in the string.
>
> However you do not have a null character in your string.
> Try :
> char *p = "my name is daniel\0";
>
> --
> Zack


I still get the same error even after putting in \0.

 
Reply With Quote
 
DanielJohnson
Guest
Posts: n/a
 
      02-18-2007
On Feb 18, 12:31 am, (E-Mail Removed) (Gordon Burditt) wrote:
> >I wrote this small program to reverse each word in the string. For
> >example: "I love You" should print as "I evoL uoY". I get Segmentation
> >Fault (core dumped) error upon running the program. It compiles fine.

>
> You stored the test string in a quoted string literal. C is permitted
> to store such data in a read-only section of the program. If you
> try writing on it, KABOOM!
>
> Note also that you keep incrementing p, losing track of where the
> beginning of the string is. That's not good when you try to print it.
>
>
>
> >// Program to reverse each word in the string
> >#include<stdio.h>

>
> >int main()
> >{
> > void reverse_string(char *, int, int);
> > char *p = "my name is daniel";
> > // keeps count of number is char in the word to reverse
> > int count = 0;
> > // keeps track of where I started this current word from
> > int current_pos = 0;
> > // Conitnue till you reach \0
> > while(1){
> > if (*p == '\0') break;
> > count = 0;
> > while(*p != ' '){
> > if (*p == '\0') break;
> > count++;
> > p++;
> > }
> > reverse_string(p, current_pos, count);
> > if (*p == ' '){
> > p++;
> > count++;
> > current_pos = count;
> > }
> > }
> > puts(p);
> > return 0;
> >}

>
> >void reverse_string(char* s, int start, int count){
> > int counter = count/2;
> > int i = 0;
> > while(i < counter){
> > *s = *(s-count);
> > s--;
> > i++;
> > }
> >}

>
> >Any suggestions......Every help is appreciated.

>
> >Thanks


What should I do prevent KABOOM...I am newbie..give me some tips...

 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      02-18-2007
>> // Program to reverse each word in the string
>> #include<stdio.h>
>>
>> int main()
>> {
>> void reverse_string(char *, int, int);
>> char *p = "my name is daniel";
>> // keeps count of number is char in the word to reverse
>> int count = 0;
>> // keeps track of where I started this current word from
>> int current_pos = 0;
>> // Conitnue till you reach \0
>> while(1){
>> if (*p == '\0') break;

>
>You correctly note that you want to continue until reaching the null
>character in the string.
>
>However you do not have a null character in your string.


>> char *p = "my name is daniel";

Yes, there is a null character terminating the above string.

>Try :
>char *p = "my name is daniel\0";


That's just a waste of memory, putting in 2 nulls.

 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      02-18-2007

On Sat, 17 Feb 2007, Zack wrote:
>
> However you do not have a null character in your string.
> Try :
> char *p = "my name is daniel\0";


If you don't know much about C yet, please try not to volunteer
"answers" to other newbies' questions. You will be wrong, and other
people in the newsgroup will have to spend extra time un-confusing
the newbie. Don't worry, there will always be other newbies --- so
you don't need to jump all over yourself to respond to each one.
You can afford to wait until you know the language better.

To the OP: Ignore Zack's post that I just quoted. Your problem
is with modifying the string literal. Put the string's characters
in an array first; then you'll be able to change their values.

-Arthur
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-18-2007
Zack wrote:
> DanielJohnson wrote:
>
>> // Program to reverse each word in the string
>> #include<stdio.h>
>>
>> int main()
>> {
>> void reverse_string(char *, int, int);
>> char *p = "my name is daniel";


>
> You correctly note that you want to continue until reaching the null
> character in the string.
>
> However you do not have a null character in your string.
> Try :
> char *p = "my name is daniel\0";


This is truly horrendous advice. You completely wrong about the absence
of a null character; Daniel's string has one by definition. And that is
not the problem. He is trying to modify a string literal. This is
fully covered in the FAQ. Both you and Daniel should read it.

Daniel needs to declare p not as a pointer to a string literal but to either
a) declare p as a character array
or
b) declare p as a pointer, for which he would then allocate memory
(via malloc) and to which allocated space he would copy the string
literal (via strcpy).
Either of these strategies would give him a modifiable char array.

 
Reply With Quote
 
Zack
Guest
Posts: n/a
 
      02-18-2007
Arthur J. O'Dwyer wrote:
> If you don't know much about C yet, please try not to volunteer
> "answers" to other newbies' questions. You will be wrong, and other
> people in the newsgroup will have to spend extra time un-confusing
> the newbie. Don't worry, there will always be other newbies --- so
> you don't need to jump all over yourself to respond to each one.
> You can afford to wait until you know the language better.

I will keep quiet.

--
Zack
 
Reply With Quote
 
Kelly
Guest
Posts: n/a
 
      02-18-2007
Zack wrote:
...snip...
> I will keep quiet.


That's much better..you messed up with the earlier question as well!
 
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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments