为什么会崩溃? [英] Why does this crash?

查看:91
本文介绍了为什么会崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在学习C并尝试在字符串数组中查找字符。我的所有

努力导致程序崩溃。谁能明白为什么?


#include< stdio.h>

int main()

{


char strArray [10] = {" abcdefg"};


int i;


int j = strlen(strArray);


for(i = 0; i< j; ++ i)

{

char a = strArray [i];


if(a =''c'')

{


printf("%s \ n",a);

返回0;

}

}


返回0;

}

解决方案

2006年9月24日,星期日, Richard Heathfield写道:


pkirk25说:


< snip>


> char a = strArray [i];

if(a =''c'')



你意思是:


if(a ==''c'')


> {

的printf(QUOT;%s\\\
&现状t;,a);



你的意思是:


printf("%c\ n,a);


它正在崩溃,因为你正在处理一个单一的字符,好像它是指向一大堆字符串中第一个字符串的

指针。



...或printf("%s \ n",strArray)。另外,OP在没有原型的情况下调用了

strlen(未定义的行为)。


德成


pkirk25写道:


大家好,


我正在学习C并试图找到一个角色字符串数组。我的所有

努力导致程序崩溃。谁能明白为什么?


#include< stdio.h>


int main()

{

char strArray [10] = {" abcdefg"};

int i;

int j = strlen(strArray);


for(i = 0; i< j; ++ i)

{

char a = strArray [i];

if(a =''c'')

{

printf("%s \ n",a);



因为%s而崩溃了代码要求你

提供一个匹配的字符串参数,但是'a''只是一个`char''

而不是字符串。一个字符串不是一个char,而是一个char的

数组,其中一个值为零以标记

字符串''结束。


另外两点:'if''语句并不是你想要的b
可能打算这样做(回到你的教科书并阅读`=''和`==''运算符之间的
差异,如果你想使用,你需要

来#include< string.hheader strlen()。


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


Richard Heathfield写道:


pkirk25说:


< snip>


> char a = strArray [i];

if(a =''c'')



你的意思是:


if(a ==''c'')



你可以避免这些问题通过调整

首先保持不变的习惯,即:


if(''c''== a)...


如果您只有一个''='',编译器会抱怨。


-

一些信息链接:

< news:news.announce.newusers

< http://www.geocities.com/nnqweb/>

< http://www.catb.org/~esr/faqs/smart-questions.html>

< http://www.caliburn.nl/topposting.html>

< http://www.netmeister.org/news/learn2quote.html>

< http://cfaj.freeshell.org/google/>


Hi all,

I''m learning C and trying to find characters in a string array. All my
efforts result in crashing programs. Can anyone see why?

#include <stdio.h>
int main()
{

char strArray[10] = {"abcdefg"};

int i;

int j = strlen(strArray);

for (i = 0; i < j; ++i)
{
char a = strArray[i];

if (a = ''c'')
{

printf("%s\n", a);
return 0;
}
}

return 0;
}

解决方案

On Sun, 24 Sep 2006, Richard Heathfield wrote:

pkirk25 said:

<snip>

>char a = strArray[i];

if (a = ''c'')


You mean:

if(a == ''c'')

>{

printf("%s\n", a);


You probably mean:

printf("%c\n", a);

It''s crashing because you''re treating a, a single char, as if it were a
pointer to the first in a whole bunch of char.

... or printf("%s\n", strArray). Also, the OP was calling
strlen without a prototype (undefined behaviour).

Tak-Shing


pkirk25 wrote:

Hi all,

I''m learning C and trying to find characters in a string array. All my
efforts result in crashing programs. Can anyone see why?

#include <stdio.h>

int main()
{
char strArray[10] = {"abcdefg"};
int i;
int j = strlen(strArray);

for (i = 0; i < j; ++i)
{
char a = strArray[i];
if (a = ''c'')
{
printf("%s\n", a);

It crashes here because the "%s" code requires that you
provide a matching string argument, but `a'' is just a `char''
and not a string. A string is not a single `char'', but an
array of `char'', one of which has the value zero to mark the
string''s end.

Two other points: The `if'' statement doesn''t do what you
probably intend (go back to your textbook and read about the
difference between the `='' and `=='' operators), and you need
to #include the <string.hheader if you want to use strlen().

--
Eric Sosman
es*****@acm-dot-org.invalid


Richard Heathfield wrote:

pkirk25 said:

<snip>

>char a = strArray[i];

if (a = ''c'')


You mean:

if(a == ''c'')

You can avoid these problems by adapting the habit of putting the
constant first, i.e.:

if (''c'' == a) ...

and the compiler will complain if you have only a single ''=''.

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>


这篇关于为什么会崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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