为什么这个简单代码中的分段错误? [英] Why segmentation fault in this simple code?

查看:71
本文介绍了为什么这个简单代码中的分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我是C语言的新手,我正在编写我的第一个简单代码。


在一个其中,我的目的是在字符串的末尾附加一个整数的ascii值

(例如101 - > e)以获得一个新的(更长的)

字符串。例如:


string:languag

letter:e

string(新值):语言

在编译时,我收到了分段错误。错误,但我不知道为什么。


这是非常简单的代码:


main ()

{

char * output =" languag";

int i = 101;

char * letter;

letter [0] =(char)i;

letter [1] =''\ 0'';

的printf(QUOT;%s\\\
",字母); //打印" e"

//下一行未执行

strcat(输出,字母); //分段错误printf("%s \ n",输出);


}


谢谢:)

Hi!

i''m a newbie in C language and i''m writing my first simple codes.

In one of these, my purpose is to append the ascii value of an interger
(example 101 --> e) at the end of a string to obtain a new (longer)
string. Example:

string: languag
letter: e
string (new value): language

In compiling, i received "Segmentation fault" error but i
can''t understand why.

This is the very simple code:

main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]=''\0'';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault printf("%s\n",output);

}

Thank you :)

推荐答案

2004年8月9日星期一11:44:08 +0000,Polar写道:
On Mon, 09 Aug 2004 11:44:08 +0000, Polar wrote:
main()
{
char * output =" languag" ;;
int i = 101;
char * letter;
letter [0] =(char)i;
letter [1] =''\''';
printf("%s \ n",letter); //打印" e"
//下一行未执行
strcat(输出,字母); //分段错误printf("%s \ n",输出);

}
main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]=''\0'';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault printf("%s\n",output);

}




抱歉错误的换行。


这是代码:


main()

{

char * output =" languag";

int i = 101;

char * letter;

letter [0] =(char)i ;

letter [1] =''\ 0'';

printf("%s \ n",letter); //打印" e"

//下一行未执行

strcat(输出,字母); //分段错误

printf("%s \ nn",输出);


}


谢谢:)



Sorry fo incorrect line-wrap.

This is the code:

main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]=''\0'';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault
printf("%s\n",output);

}

Thank you :)


2004-08-09,Polar< po *** @ pole.po>写道:
On 2004-08-09, Polar <po***@pole.po> wrote:
嗨!

在编译时,我收到了分段错误。错误,但我不明白为什么。


可能不是在编译中但是在跑步中,但无论如何。

main()
{char / output =" languag" ;;
int i = 101;
char * letter;
^^^^^^^


这是一个指向角色的指针。目前,它指向计算机内存中的随机

地址。它指的是

可能(并且可能)不属于地址的地址。到你的程序。或者它可以

指向存储其他有用信息的地址,例如程序代码或程序的其他变量,或者

任何东西。

letter [0] =(char)i;
letter [1] =''\ 0'';
Hi!

In compiling, i received "Segmentation fault" error but i
can''t understand why.

Probably not "in compiling" but "in running", but anyway.
main()
{
char* output="languag";
int i = 101;
char* letter; ^^^^^^^

This is a pointer to a character. Currently it points to a random
address inside the computer''s "memory". It points to an address that
possibly (and probably) does not "belong" to your "program". Or it can
point to an address where other usefull information is stored, like
the code of your program, or other variables of your program, or
anything.
letter[0]=(char)i;
letter[1]=''\0'';




现在你试着写一些东西给这个随机地址(可能

甚至不属于你),以及旁边的地址。一个

段错误迫在眉睫!


/ npat



Now you try to write something to this random address (that probably
doesn''t even belong to you), as well as to the address next to it. A
segfault is imminent!

/npat


Polar在09/08/04写道:
Polar wrote on 09/08/04 :
main()
{char / output =" languag" ;;


字符串文字不保证是可写的。最好定义

它们是只读的:

char const * output =" languag";

int i = 101 ;


这个神奇的价值应该代表什么?如果你想要''e'',只需使用''e''


char * letter;


警告。此变量未初始化,这意味着其值未确定为
。作为一个指针,它指向任何地方。

letter [0] =(char)i;


解除引用未定义的指针会调用未定义的行为(UB)。

你的程序已经死了。修复它。

letter [1] =''\''';


再次UB。

printf("%s \ n,字母); //打印e


再次UB。 (将未定义的值传递给函数)

//未执行下一行
strcat(输出,字母); //分段错误


UB又出于多种原因:


- ''letter''仍未定义

- ''输出''不可写

- 如果它是可写的,那么e没有足够的空间

printf("%s \ n",output);
}
main()
{
char* output="languag";
string literals are not guaranteed to be writable. Better to define
them read-only:

char const * output="languag";
int i = 101;
What is this magic value supposed to represent? If you want ''e'', just
use ''e''.
char* letter;
Warning. This variable is not initialized, meaning that its value is
undetermined. Being a pointer, it points anywhere.
letter[0]=(char)i;
Dereferencing an undefined pointer invokes an undefined behaviour (UB).
Your program is dead. Fix it.
letter[1]=''\0'';
UB again.
printf("%s\n",letter); //prints "e"
UB again. (Passing an undefined value to a function)
//next line is not executed
strcat(output,letter); //segmentation fault
UB again for many reasons:

- ''letter'' is still undefined
- ''output'' is not writable
- Should it be writable, there is no room enough for "e"
printf("%s\n",output);
}




你必须使用数组oc ahrs来做你想要的。记忆空间不会是魔法来的。$ />

-

Emmanuel

C-FAQ: http://www.eskimo.com /~scs/C-faq/faq.html


C是一个敏锐的工具



You must use array oc ahrs to do what you want. Memory space doesn''t
come by magic.

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

"C is a sharp tool"

这篇关于为什么这个简单代码中的分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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