分段故障(核心转储) [英] Segmentation Fault (core dumped)

查看:78
本文介绍了分段故障(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个小程序来反转字符串中的每个单词。对于

例子:我爱你应打印为I evoL uoY。我得到分段

运行程序时出现故障(核心转储)错误。它编译得很好。


//程序来反转字符串中的每个单词

#include< stdio.h>


int main()

{

void reverse_string(char *,int,int);

char * p =" my名字是daniel" ;;

//保持数字的数量是要反转的字的字符数

int count = 0;

//保持跟踪我从哪里开始这个当前的单词来自

int current_pos = 0;

// Conitnue直到你达到\0

而(1 ){

如果(* p ==''\''')休息;

count = 0;

while(* p !=''''){

如果(* p ==''\''')休息;

count ++;

p ++;

}

reverse_string(p,current_pos,count);

if(* p ==''''){

p ++;

count ++;

current_pos = count;

}

}

put(p);

返回0;

}


void reverse_strin g(char * s,int start,int count){

int counter = count / 2;

int i = 0;

while (i<柜台){

* s = *(s-count);

s--;

i ++;

}

}

任何建议......感谢所有帮助。


谢谢

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

推荐答案

DanielJohnson写道:
DanielJohnson wrote:

//程序来反转字符串中的每个单词

#include< stdio.h>


int main()

{

void reverse_string( char *,int,int);

char * p ="我的名字是daniel" ;;

//保持数字的数字是要反转的字的字符

int count = 0;

//跟踪我从哪里开始这个当前单词

int current_pos = 0;

// Conitnue直到你达到\0

而(1){

if(* p ==''\''')break;
// 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;



你正确地注意到你想要继续直到达到字符串中的null

字符。


但是你的字符串中没有空字符。

尝试:

char * p ="我的名字是daniel \ 0" ;;


-

Zack

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 wrote this small program to reverse each word in the string. For

>例如:我爱你应打印为I evoL uoY。运行程序时出现Segmentation
Fault(core dumped)错误。它汇编得很好。
>example: "I love You" should print as "I evoL uoY". I get Segmentation
Fault (core dumped) error upon running the program. It compiles fine.



您将测试字符串存储在带引号的字符串文字中。 C允许

将这些数据存储在程序的只读部分。如果你用b
尝试写它,KABOOM!


还要注意你继续增加p,失去对

的追踪字符串的开头是。当你尝试打印它时,这并不好。

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.


>
//程序来反转字符串中的每个单词
#包括< stdio.h>



void reverse_string(char *,int,int);
char * p ="我的名字是daniel" ;;
//保持数字的数量是单词中的字符反转
int count = 0;
//跟踪我从哪里开始这个当前单词来自
int current_pos = 0;
// Conitnue直到你到达\0
而(1){
如果(* p ==''\''')中断;
count = 0;
while(* p!=''''){
if if(* p ==''\''')break;
count ++;
p ++;
}
reverse_string(p,current_pos,count);
if(* p ==''''){
p ++;
count ++;
current_pos = count;
}
} put(p);
返回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 ++;
}
}

任何建议......感谢每一位帮助。

谢谢
>
// 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



2月18日上午12:29,Zack< goldszs ... @ gmail.comwrote:
On Feb 18, 12:29 am, Zack <goldszs...@gmail.comwrote:

DanielJohnson写道:
DanielJohnson wrote:

//反转字符串中每个单词的程序

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


int main()

{

void reverse_string(char *,int,int );

char * p ="我的名字是daniel" ;;

//保持数字的数字是字母中的字母来反转

int count = 0;

//跟踪我从哪里开始这个当前单词

int current_pos = 0;

// Conitnue直到你达到\0

而(1){

如果(* p ==''\''')中断;
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;



你正确地注意到你要继续直到达到字符串中的null

字符。


但是你的字符串中没有空字符。

尝试:

char * p ="我的名字是daniel \ 0" ;;


-

Zack


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



即使放入\0后仍然会出现相同的错误。

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


这篇关于分段故障(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆