字符数组 [英] Character arrays

查看:82
本文介绍了字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




前几天我在编码时发现了一些事情:


如果我将字符数组声明为char s [0],并尝试使用它作为任何

其他字符数组..它在大多数情况下完美无缺。它

包含任意长度的字符串。我想这里发生的事情是

这个数组最初只保留''\0'',因此长度为1.


但有时,当我尝试编写一些函数并对这些字符数组进行一些操作时,行为是不稳定的......当我试图存储时,它们会导致分段错误某些

值。


我无法理解为什么这些数组表现得像

这个......你能解释一下这些阵列增长了什么可能会发生什么?


谢谢

Anitha

Hi

I observed something while coding the other day:

if I declare a character array as char s[0], and try to use it as any
other character array..it works perfectly fine most of the times. It
holds strings of any length. I guess what is happening here is that
this array initially holds only ''\0'' and hence is of length 1.

But sometimes, when I tried to write some functions and do some
manipulations on such character arrays, the behavior is erratic..they
result in segmentation fault sometimes,when I try to store certain
values.

I am not able to understand why these arrays are behaving like
this..can you explain how these arrays grow and what could be
happening?

Thanks
Anitha

推荐答案

2004年11月6日07:02:49 -0800
a。** ****@gmail.com (Anitha)写道:
On 6 Nov 2004 07:02:49 -0800
a.******@gmail.com (Anitha) wrote:
前几天我在编码时发现了一些事情:

如果我宣布一个字符数组为char s [0],并尝试将其用作任何其他字符数组。它的工作原理非常精细时代的。它拥有任何长度的字符串。我想这里发生的事情是这个数组最初只保留''\0'',因此长度为1.


不,这意味着你在做标准不允许的东西

AFAIK。您正在声明一个0长度的数组。如果编译器允许

这个作为扩展,它可能实际上允许做另一个

技巧。

但有时,当我试图写一些函数并对这些字符数组进行一些操作,行为不稳定......当我试图存储某些值时,它们有时会导致分段错误。
I observed something while coding the other day:

if I declare a character array as char s[0], and try to use it as any
other character array..it works perfectly fine most of the times. It
holds strings of any length. I guess what is happening here is that
this array initially holds only ''\0'' and hence is of length 1.
No, it means you are doing something that is not allowed by the standard
AFAIK. You are declaring an array of 0 length. If the compiler allows
this as an extension it is probably actually allowed to do another
trick.
But sometimes, when I tried to write some functions and do some
manipulations on such character arrays, the behavior is erratic..they
result in segmentation fault sometimes,when I try to store certain
values.

I am not able to understand why these arrays are behaving like
this..can you explain how these arrays grow and what could be
happening?




病人:我这样做会很疼..

博士:嗯,不要那么做。


数组不是'不会成长。发生的事情是你覆盖了其他数据,有时甚至看不到任何影响。


如果有什么有时工作然后你几乎总是在做一些你不应该做的事情。如果故障类似于分段

故障,那么你绝对是*做一些你不应该做的事情。
做。如果它似乎有效,但你无法理解它为什么会起作用那么

几乎肯定会在最糟糕的时候失败,例如当你的老板向你展示它时VIP。

-

Flash Gordon

有时我觉得拍摄对某些人来说太好了。

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。



Patient: It hurts when I do this..
Dr: Well, don''t do that then.

The array isn''t growing. What is happening is that you are overwriting
other data and sometimes not seeing any effect.

If anything "works sometimes" then you are almost always doing something
you should not be doing. If the failure is something like segmentation
faults then you are *definitely* doing something you should not be
doing. If it appears to work but you cannot understand why it works then
it is almost certain to fail at the worst possible time, such as when
your boss is demonstrating it to a VIP.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.




Anitha <一个****** @ gmail.com>。写了

"Anitha" <a.******@gmail.com> wrote

我前几天在编码时发现了一些东西:

如果我将一个字符数组声明为char s [0],并尝试将其用作任何字符串
其他角色阵列。大部分时间都可以完美运行。它拥有任何长度的字符串。我想这里发生的事情是这个数组最初只保留''\0'',因此长度为1.

数组的长度为零字节。然而,与它相邻的数据很可能是零,因此是空字符串的错觉。


char empty [1] = {' '/ 0''};


是一种在C中去除空字符串的非常简单的方法。
但有时,当我尝试编写一些函数并做一些<对这样的字符数组进行操作时,行为是不稳定的......当我试图存储某些值时,它们会导致分段错误。

因为你是什么做是非法的。
我无法理解为什么这些阵列表现得像这样......你能解释这些阵列如何成长以及可能发生什么?

I observed something while coding the other day:

if I declare a character array as char s[0], and try to use it as any
other character array..it works perfectly fine most of the times. It
holds strings of any length. I guess what is happening here is that
this array initially holds only ''\0'' and hence is of length 1.
The array would be zero bytes in length. However it is highly likely that
the data adjacent to it is zero, hence the illusion of an empty string.

char empty[1] = {''/0''};

is a perfectly unexceptional way of decalring an empty string in C.
But sometimes, when I tried to write some functions and do some
manipulations on such character arrays, the behavior is erratic..they
result in segmentation fault sometimes,when I try to store certain
values.
Because what you are doing is illegal.
I am not able to understand why these arrays are behaving like
this..can you explain how these arrays grow and what could be
happening?



你需要查找指针。 C不是Java,其中数组可以从

复制到另一个,并且可以为null。 C数组保留一个内存区域,并且

地址无法更改。当你想要一个变量时,使用指针

有时是一个数组,有时是另一个数组。


You need to look up pointers. C is not Java, where arrays can be copied from
one to the other and can be null. A C array reserves an area of memory, and
the address cannot be changed. Pointers are used when you want a variable to
be one array sometimes and another array at other times.


Malcolm写道:
Malcolm wrote:

Anitha <一个****** @ gmail.com>。写了

"Anitha" <a.******@gmail.com> wrote

我前几天在编码时发现了一些东西:

如果我将字符数组声明为char s [0],


标准C中没有零大小的对象。

我猜你正在使用扩展名。

大部分时间都很好。


当它工作时,

它就像把车停在别人的空间里

当你穿上它的时候不会被抓住。

它包含任意长度的字符串。

I observed something while coding the other day:

if I declare a character array as char s[0],
There are no zero size objects in standard C.
I''m guessing you are using an extension.
fine most of the times.
When it works,
it''s like parking your car in somebody else''s space
when you don''t get caught.
It holds strings of any length.




如果您尝试索引到内存中未分配给您的程序,

然后您的代码不再受C规则的约束。

任何事情都可能发生。它可以按你想要的方式工作。


-

pete



If you try to index into memory that is not allocated for your program,
then your code is no longer governed by the rules of C.
Anything can happen. It can work the way you want it to, or not.

--
pete


这篇关于字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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