不同的固定宽度的字符串和零结尾的字符串 [英] difference fixed width strings and zero-terminated strings

查看:140
本文介绍了不同的固定宽度的字符串和零结尾的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC 4.4.4 C89

gcc 4.4.4 c89

我进入关于固定宽度的字符串,零结尾的字符串。

I got into a recent discussion about "fixed width strings" and "zero terminated strings".

当我想一想。他们似乎是同样的事情。一个字符串,具有终止空。

When I think about this. They seem to be the same thing. A string with a terminating null.

char *name = "Joe bloggs";

时,不能改变一个固定的宽度串。并且还具有终止空。

Is a fixed width string that cannot be changed. And also has a terminating null.

另外,在讨论中,我被告知,函数strncpy应该从未在零结尾的字符串'使用。

Also in the discussion I was told that strncpy should never been used on 'zero terminated strings'.

非常感谢任何susgestions,

Many thanks for any susgestions,

推荐答案

术语固定宽度串通常是指完全不同的东西。

The term "fixed-width string" usually refers to something completely different.

一个固定宽度的字符串是完全相同 N 字符,所有的<$ C字符串$ C> N 字符被保证被初始化。如果你想重新present较短的字符串,你要垫在结束零个字符您的字符串。你要尽可能多的零字符加在必要时使用的所有 N 字符。请注意,如果你需要存储长度的字符串完全 N ,固定宽度的字符串将具有的没有零字符的结尾。即一般情况下,固定宽度的字符串的的零终止!

A fixed-width string of with N is a string of exactly N characters, where all N characters are guaranteed to be initialized. If you want to represent a shorter string, you have to pad your string with zero characters at the end. You have to add as many zero characters as necessary to use up all N characters. Note, that if you need to store a string of length exactly N, a fixed-width string will have no zero character at the end. I.e. in general case fixed-width strings are not zero terminated!

这样做有什么目的?这样做的目的是存储最大可能长度的字符串时保存1个字符。如果您使用的是宽 N 的固定宽度的字符串,那么您需要的 N 字符重新present一个长度字符串 N 。相比之下,普通的零结尾的字符串,这需要 N + 1 字符(0终结额外的字符)。

What is the purpose of this? The purpose of this is to save 1 character when storing the string of maximum possible length. If you are using fixed width strings of width N, then you need exactly N characters to represent a string of length N. Compare that to ordinary zero-terminated strings, which would require N + 1 character (extra character for zero terminator).

为什么在末尾补零?它是用零填充,以简化固定宽度的字符串字典比较。您只需比较所有 N 字符,直到你打的差别。注意,人们可以使用绝对任何字符到垫固定宽度串全长。只要确保你得到正确的字典序。使用零字符填充是一个不错的选择,虽然。

Why is it padded with zeros at the end? It is padded with zeros to simplify lexicographic comparison of fixed-width strings. You simply compare all N characters until you hit the difference. Note, that one can use absolutely any character to pad the fixed-width string to full length. Just make sure that you get the right lexicographic ordering. Using zero character for padding is a good choice though.

当是它有用吗?非常稀有。通过固定宽度的字符串提供的储蓄是通用的字符串处理很少重要的是:这些节约太小,当全宽度所使用的唯一的字符串的情况下发生。但是,他们可能来自有用的是一些具体情况。

When is it useful? Very rarely. The savings provided by fixed-width strings are rarely important in generic string processing: these saving are too small and only occur in cases when the full width is used by the string. But they might come useful is some specific cases.

如果这一切是从哪里来的? 固定宽度的字符串的一个典型的例子是在一些老版本的Unix文件系统的14个字符范围的文件名称字段。它被重新由14个字符数组,固定宽度重新presentation psented $ P $使用。当时节省1个字符的全长(所有14个字符)的文件名是很重要的。

Where does all this come from? A classic example of a "fixed-width string" is a 14-char wide file name field in some old version of Unix file system. It was represented by an array of 14 chars and fixed width representation was used. At that time saving 1 character on full-length (all 14 characters) file name was important.

现在到函数strncpy 。功能函数strncpy 是专门介绍了该文件系统初始化的14个字符宽的文件名的字段。功能函数strncpy 是专为生成有效的固定宽度的字符串:它执行零结尾的字符串转换成固定宽度的字符串。不幸的是,它被赋予了误导性的名字,这也是为什么今天很多人误以为是零结尾的字符串安全的复制功能。后者是函数strncpy 目的和功能是完全不正确的认识。

Now to strncpy. Function strncpy was specifically introduced for initializing those 14-character wide file name fields in that file system. Function strncpy was specifically created to generate a valid fixed-width string: it performs conversion of zero-terminated string into a fixed-width string. Unfortunately, it was given a misleading name, which is the reason why many people today mistake it for a "safe" copying function for zero-terminated strings. The latter is a totally incorrect understanding of strncpy purpose and functionality.

使用字符串重新present固定宽度的字符串(如你的例子)是不是一个好主意,因为字符串总是在最后加一个零字符,和固定宽度的字符串不一定做它。这是怎样一群固定宽度的字符串可以在C程序进行初始化

Using string literals to represent fixed-width strings (as in your example) is not a good idea, since string literals always add a zero character at the end, and fixed-width strings don't necessarily do it. This is how a bunch of fixed width strings can be initialized in a C program

char fw_string1[7] = { 'T', 'h', 'i', 's', ' ', 'i', 's' };
char fw_string2[7] = { 's', 't', 'r', 'i', 'n', 'g' };
char fw_string3[7] = { 'H', 'e', 'l', 'l', 'o' };

所有的数组中有相同数量的元素 - 7。注意,第一个字符串是不是零结尾的,其余都是零填充。 普通的字符串转换成固定宽度的人会看起来如下:

All arrays have the same number of elements - 7. Note, that the first string is not zero-terminated, while the rest are zero-padded. Conversion of "ordinary" string into a fixed-width one will look as follows

char fw_string4[7];

strncpy(fw_string4, "Hi!", 7);

在这种情况下,功能函数strncpy 使用正是它的目的是用于

In this case function strncpy is used exactly what it was intended to be used for.

请还,那一边。基本上,你必须把它们作为原始字符数组,并手动实现任何更高级别的操作。最基本的操作会被自然地从功能纪念品... 组实施。 memcmp ,对于一个例子,将实施比较。

Keep in mind also, that aside from the conversion function strncpy, standard library provides virtually no means for working with fixed-width strings. You basically have to treat them as raw character arrays, and implement any higher-level operations manually. Most basic operations will be naturally implemented by functions from mem... group. memcmp, for one example, will implement comparison.

P.S。事实上,考虑到咖啡馆的评论,在C语言中可以使用字符串初始化固定宽度的字符串,因为C语言允许字面初始化是一个字符长度超过数组(即C中,它被确定,如果终止零呢不适合入阵)。所以,上述可等效地改写为

P.S. Actually, taking into account caf's comment, in C language one can use string literals to initialize fixed-width strings, since C language allows the literal initializer to be one character longer then array (i.e. in C it is OK, if the terminating zero does not fit into the array). So, the above can be equivalently rewritten as

char fw_string1[7] = "This is";
char fw_string2[7] = "string";
char fw_string3[7] = "Hello";

注意 fw_string1 仍然不是零结尾在这种情况下。

Note that fw_string1 is still not zero-terminated in this case.

这篇关于不同的固定宽度的字符串和零结尾的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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