如果最大字符容量为256,如何存储1000个字符数组? [英] if the maximum capacity of character is 256 how to store character array of 1000?

查看:169
本文介绍了如果最大字符容量为256,如何存储1000个字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果字符的最大容量为256,如何存储1000的字符数组
是否可以声明:
char s [1000];

If the maximum capacity of character is 256 how to store character array of 1000? is it possible to declare: char s[1000];

推荐答案

是的,肯定是

char s[1000];

您可以将1000视为数组的长度,将256视为宽度。您得到1000个 char s数组。每个 char 是8位(至少在您使用的计算机上),因此可以存储256个不同的值。 (实际上,将宽度视为8,而不是256可能更合适。)

You can think of 1000 as the "length" of the array and 256 as the "width". You get an array of 1000 chars. Each char is 8 bits (on the machine you're using, at least), and can therefore store 256 distinct values. (And, actually, it would probably be more appropriate to think of the "width" as being 8, not 256.)

这是您的数组,每个框代表一个 char

Here is your array, with each box representing one char:

   +---+---+---+---+---+---+---+---+-   -+---+
s: |   |   |   |   |   |   |   |   | ... |   |
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999

或者在这里显示单个位:

Or here it is showing the individual bits:

   +---+---+---+---+---+---+---+---+-   -+---+
s: |   |   |   |   |   |   |   |   |     |   | 7
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 6
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 5
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 4
   +---+---+---+---+---+---+---+---+-   -+---+    bit
   |   |   |   |   |   |   |   |   | ... |   | 3  number
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 2
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 1
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 0
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999
                array index

假设我们在数组,方法是调用 strcpy

Suppose we put a string in the array, either by calling strcpy:

strcpy(s, "Hello!");

或我在声明它时对其进行初始化:

or my initializing it when we declare it:

char s[1000] = "Hello!";

按字节显示,如下所示:

By bytes it looks like this:

   +---+---+---+---+---+---+---+---+-   -+---+
s: | H | e | l | l | o | ! |\0 |   | ... |   |
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999

或者按位看起来像这样:

Or by bits it looks like this:

   +---+---+---+---+---+---+---+---+-   -+---+
s: | 0 | 0 | 0 | 0 | 0 | 0 | 0 |   |     |   | 7
   +---+---+---+---+---+---+---+---+-   -+---+
   | 1 | 1 | 1 | 1 | 1 | 0 | 0 |   |     |   | 6
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 1 | 1 | 1 | 1 | 0 |   |     |   | 5
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 0 | 0 | 0 | 0 | 0 | 0 |   |     |   | 4
   +---+---+---+---+---+---+---+---+-   -+---+    bit
   | 1 | 0 | 1 | 1 | 1 | 0 | 0 |   | ... |   | 3  number
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 1 | 1 | 1 | 0 | 0 |   |     |   | 2
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 0 | 0 | 0 | 1 | 0 | 0 |   |     |   | 1
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 0 | 0 | 1 | 1 | 0 |   |     |   | 0
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999
                array index

数组剩余。

[PS到nitpickers:是的,它们是ASCII码,否,C标准未指定字符编码。但是我认为我们可以放心地假设那些是发问者会看到的代码。]

[P.S. to nitpickers: Yes, those are ASCII codes, and no, character encoding is not specified by the C Standard. But I think we can safely assume that those are the codes the questioner would see.]

这篇关于如果最大字符容量为256,如何存储1000个字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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