Segmentation Fault (core dumped)

D

DanielJohnson

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
 
Z

Zack

DanielJohnson said:
// 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";
 
G

Gordon Burditt

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

DanielJohnson

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

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

DanielJohnson

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.

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

Gordon Burditt

// Program to reverse each word in the string
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.
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.
 
A

Arthur J. O'Dwyer

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
 
M

Martin Ambuhl

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

Zack

Arthur said:
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.
 
R

Richard Heathfield

DanielJohnson said:

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

Own the memory you use. There's more to it than just making the data
writeable, however. I changed your program to use writeable data, and
it still blows up. Here is a version that doesn't. Read, mark, learn,
and inwardly digest.

#include <stdio.h> /* note the space between ude and <std */

/* function prototypes belong at file scope */
void reverse_substring(char *s,
char *e);

int main(void) /* proper declarator for main */
{
/* own the data */
char data[] = "my name is daniel";

/* keep two pointers - p will track the
beginning of a word, q the end */

char *p = data;
char *q = data;

/* stop when we hit the end */
while(*p != '\0')
{
/* skip past leading spaces */
while(*p == ' ')
{
++p;
}
q = p;
while(*q != '\0' && *q != ' ')
{
++q;
}
if(q > p)
{
/* we've found the end of a word, so reverse it */
reverse_substring(p, q - 1);

/* point to start of next word */
p = q;
}
}

/* write the result */
puts(data);
return 0;
}

void reverse_substring(char *s,
char *e)
{
char t = 0;

while(s < e)
{
t = *s;
*s++ = *e;
*e-- = t;
}

/* ain't that a lot simpler? :) */

return;
}
 
K

Keith Thompson

DanielJohnson said:
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. [snip]
char *p = "my name is daniel"; [snip]
reverse_string(p, current_pos, count);

The comp.lang.c FAQ is at <http://www.c-faq.com/>. You've asked
question 1.32.
 
B

Bill Pursell

I will keep quiet.


I don't think that's a good idea. Two proven methods for
learning any subject are: 1) make mistakes, 2) teach someone
else. If you combine those two by making errors while you
try to teach someone, there is only a danger
if you have no one to correct your mistakes.
There is no shortage of such people on c.l.c. If you wait
until you have full mastery of a subject before you try
to answer someone's question, you will never answer
anyone's questions.

It is a good idea, however, to make an effort to ensure
that your replies are correct. A very good way to
gauge that you understand something is to post an explanation
here and see if it gets trounced.
 
D

DanielJohnson

DanielJohnson said:

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

Own the memory you use. There's more to it than just making the data
writeable, however. I changed your program to use writeable data, and
it still blows up. Here is a version that doesn't. Read, mark, learn,
and inwardly digest.

#include <stdio.h> /* note the space between ude and <std */

/* function prototypes belong at file scope */
void reverse_substring(char *s,
char *e);

int main(void) /* proper declarator for main */
{
/* own the data */
char data[] = "my name is daniel";

/* keep two pointers - p will track the
beginning of a word, q the end */

char *p = data;
char *q = data;

/* stop when we hit the end */
while(*p != '\0')
{
/* skip past leading spaces */
while(*p == ' ')
{
++p;
}
q = p;
while(*q != '\0' && *q != ' ')
{
++q;
}
if(q > p)
{
/* we've found the end of a word, so reverse it */
reverse_substring(p, q - 1);

/* point to start of next word */
p = q;
}
}

/* write the result */
puts(data);
return 0;

}

void reverse_substring(char *s,
char *e)
{
char t = 0;

while(s < e)
{
t = *s;
*s++ = *e;
*e-- = t;
}

/* ain't that a lot simpler? :) */

return;

}


Thanks for such an elegant solution. I learned two things about string
literal. I apologize that I didn't look up C FAQs before.

I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.
Can you name a few which can help speed up my progress.

thanks
 
R

Richard Heathfield

DanielJohnson said:

I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.

No, but it's "best of breed".
Can you name a few which can help speed up my progress.

Clarity of thought is the best debugger there is. I've spent way too
much time stepping uselessly through code, one line at a time. A penny
of brainwork is worth a pound of code-stepping.

But on the rare occasions when I do use a debugger, gdb is the one I
use. On Linux, I see no more reason to use some other debugger instead
of gdb than I do to use some other C compiler instead of gcc.
 
T

tphipps1

I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.
Can you name a few which can help speed up my progress.

An afternoon spent learning gdb will "speed up your progress" day in,
day out for the rest of your life.
 
K

Keith Thompson

DanielJohnson said:
I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.
Can you name a few which can help speed up my progress.

<OT>
"ddd" is a graphical frontend to gdb; it can also be used with other
debuggers.
</OT>
 
C

Charlton Wilbur

BP> It is a good idea, however, to make an effort to ensure that
BP> your replies are correct. A very good way to gauge that you
BP> understand something is to post an explanation here and see if
BP> it gets trounced.

Posting incorrect information here is not "an effort to ensure that
your replies are correct," however. That involves going to the FAQ
and to the good C references (K&R, the Standard, and Harbison &
Steele) to ensure that you're correct *before* you post.

That said, it's been nearly 20 years since I started programming in C,
and I'm still really gratified when I post code here that nobody finds
flaw in....

Charlton
 
M

Malcolm McLean

Richard Heathfield said:
DanielJohnson said:



No, but it's "best of breed".


Clarity of thought is the best debugger there is. I've spent way too
much time stepping uselessly through code, one line at a time. A penny
of brainwork is worth a pound of code-stepping.

But on the rare occasions when I do use a debugger, gdb is the one I
use. On Linux, I see no more reason to use some other debugger instead
of gdb than I do to use some other C compiler instead of gcc.
My problem is that I am always switching systems.
Once you get into a compile / debug habit, it is a real nuisance to have to
adapt to a different debugger. So generally I just don't use them.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top