C 中的 char[] 操作
假设我定义以下内容:
unsigned char temp[2][13];
分配给此 char[] 的默认值是什么?是像 {'', '', '', ... , ''}
吗?
在我的程序(计算器)中,我有 operand[2][13]
(其中第一个操作数是 operand[0]
,第二个操作数是 operand[1 ]
),我有操作和Result[15]
,它们都是unsigned char
。首先,用户输入 Operand1 和 Operand2(一次一个字符),并将其存储在 operand[2][13]
中,然后程序会将结果作为字符存储在 Result 中[15]
。现在我想要的是,在下一次迭代中,我想清除 operand[2][13]
、操作和 Result[15]
以便用户可以输入下一个操作数程序执行下一步操作并将结果保存在Result[15]
中。
我怎样才能清除这些数组?
Suppose I define the following:
unsigned char temp[2][13];
What is the default values will be assign to this char[]
? Is it like {'', '', '', ... , ''}
?
In my program (The calculator), I have operand[2][13]
(where first operand is operand[0]
and second operand is operand[1]
) and I have Operation and Result[15]
and they are all unsigned char
. Fisrt, the user will enter Operand1 and Operand2 (A char at a time) and it will be stores in operand[2][13]
then the program will store the result as chars in Result[15]
. Now what I want is, at next iteration, I want to clear operand[2][13]
, operation and Result[15]
so that the user can enter next operands and the program executes next operation and saves the result in Result[15]
.
How can I clear these arrays?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
memset
(操作数, 0, 2 * 13 );
memset
(结果, 0, 15);
memset
(operand, 0, 2 * 13);
memset
(Result, 0, 15);
无默认值。一点也没有。确保您自己设置它们。这是一种方便的表格:
No default values. None at all. Make sure you set them yourself. Here's one handy form:
''
不是一个值。事实上,指定一个“空”字符是没有意义的。通常数据不会为您初始化。数据将是分配之前内存中的任何数据。例外情况是,如果这是静态数据,则应将其清零('\0')。
您通常应该计划使用
memset()
等函数自行初始化此类数据。''
is not a value. In fact, it does not make sense to specify an "empty" character.Usually the data is not initialized for you. The data will be whatever was in the memory before you allocated it. The exception is if this is static data, then is should be zero'd out ('\0').
You should generally plan on initializing such data yourself using a function such as
memset()
.这取决于定义它的上下文。
如果它是在函数之外(全局)声明的,它将驻留在可执行文件的 .bss 部分,操作系统将在程序启动时、main 之前将整个数组初始化为零 被调用。
如果它是在本地(在堆栈上)声明的,它将包含那里的任何垃圾,然后您可以使用 memset 或适当的数组初始化程序来初始化它。
It depends on the context where it is defined.
If it is declared out of a function (globally), it will reside in the
.bss
section of the executable and the operating system will initialise the whole array to zero on program startup, beforemain
is invoked.If it's declared locally (on the stack) it will contain whatever garbage is there and then you can either initialise it with
memset
or with the appropriate array initialiser.要么没有为数组元素分配默认值(读取值是未定义的行为),要么分配 0(零),具体取决于数组的定义方式和位置。
全局和静态数组初始化为 0(零)
您可以根据自己的喜好进行初始化
上述初始化的效果是,除了
tmp[0][0]
到之外,所有元素均为 0(零) >tmp[0][4]
和tmp[1][0]
。或者在初始化后在代码中间一一设置元素:
Either there is no default value assigned to the array elements (and it's undefined behaviour to read the values) or 0 (zero) is assigned, depending on exactly how and where the array is defined.
global and static arrays are initialized to 0 (zero)
You can initialize with your preference though
The above initialization has the effect that all elements are 0 (zero) except
tmp[0][0]
totmp[0][4]
andtmp[1][0]
.Or set the elements one by one in the middle of your code, after initialization: