通过索引访问char *的总线错误?! [英] bus errors accessing a char * by index?!

查看:62
本文介绍了通过索引访问char *的总线错误?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚为什么这段代码拒绝工作:


char * s =" hello";

printf( "%s",s [0]);


i在运行时遇到总线错误。 whyyyyyy?


i在尝试strncpy等时也会遇到同样类型的错误。


请帮助。

i can''t seem to figure out why this code refuses to work:

char *s = "hello";
printf("%s", s[0]);

i keep getting bus errors when i run this. whyyyyyy?

i also get the same type of errors when trying to do strncpy and such.

please help.

推荐答案

在文章< 11 ********************** @ e3g2000cwe.googlegroups 。 com>,

< cr ******* @ gmail.comwrote:
In article <11**********************@e3g2000cwe.googlegroups. com>,
<cr*******@gmail.comwrote:

>我看上去不行弄清楚为什么这段代码拒绝工作:

char * s =" hello";
printf("%s",s [0]);
<当我运行这个时,我不断收到总线错误。 whyyyyyy?

我在尝试strncpy等时也遇到了同样的错误。

请帮忙。
>i can''t seem to figure out why this code refuses to work:

char *s = "hello";
printf("%s", s[0]);

i keep getting bus errors when i run this. whyyyyyy?

i also get the same type of errors when trying to do strncpy and such.

please help.



听起来像是有缺陷的RAM芯片。你需要买一台新电脑。

Sounds like a defective RAM chip. You need to buy a new computer.


cr * ******@gmail.com 写道:

i似乎无法弄清楚为什么这段代码拒绝工作:


char * s =" hello";

printf("%s",s [0]);
i can''t seem to figure out why this code refuses to work:

char *s = "hello";
printf("%s", s[0]);



您使用的是%s格式说明符,用于打印C

字符串,即指向空终止序列的指针chars。


s [0],在你的例子中,是一个字符。


看到问题?

[提示:%c格式说明符有什么作用?]

You are using the %s format specifier, which is used to print a C
string, i.e. a pointer to a null terminated sequence of chars.

s[0], in your example, is a char.

See the problem?

[hint: what does the %c format specifier do?]


>

i当我遇到总线错误跑这个。 whyyyyyy?


i在尝试strncpy等时也会遇到同样类型的错误。


请帮忙。
>
i keep getting bus errors when i run this. whyyyyyy?

i also get the same type of errors when trying to do strncpy and such.

please help.



HTH,

--ag

-

Artie Gold - 奥斯汀,德克萨斯州
http://goldsays.blogspot.com

你不能亲吻*除非你想念**

[* - 保持简单,愚蠢。 ** - 简单,愚蠢。]

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can''t KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]



char * s =" hello";

printf("%s",s [0]);


i在运行时遇到总线错误。 whyyyyyy?
char *s = "hello";
printf("%s", s[0]);

i keep getting bus errors when i run this. whyyyyyy?



您可能想要这样做:

printf("%s",s); / *打印整个字符串* /



printf("%s",& s [0]); / *打印整个字符串;另一种说法是

同样的事情* /



printf("%c",s [0]); / *只打印第一个字符。 * /


以下几行正在发生(虽然这是定义的实现
,所以可能不是这样):


让我们说s存储在内存位置0x123456

我的版本1说''打印从内存位置开始的字符串

0x123456' '

我的版本2说''打印从内存位置开始的字符串

这是s''的第一个字符的地址(反过来,

0x123456,这很好。

我的版本3说''打印字符s [0]''''h''是

ascii 0x68。

现在这里是踢球者:

你的版本说''打印从内存位置开始的字符串

0x68。''但谁知道什么是0x68?可能是垃圾。或者更糟。


Michael

You probably want to do either:
printf("%s", s); /* print the whole string */
or
printf("%s", &s[0]); /* print the whole string; another way to say
the same thing */
or
printf("%c", s[0]); /* print just the first character. */

Something along the following lines is happening (although this is
implementation defined, so it may not be exactly this):

Let''s say s is stored at memory location 0x123456
My version 1 says ''print the string starting at memory location
0x123456''
My version 2 says ''print the string starting at the memory location
that is the address of the first character of s'' (which is, in turn,
0x123456, which is fine).
My version 3 says ''print the character s[0]'' which is ''h'' which is
ascii 0x68.
Now here''s the kicker:
Your version says ''print the string starting at memory location
0x68.'' But who knows what is at 0x68? Probably garbage. Or worse.

Michael


这篇关于通过索引访问char *的总线错误?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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