用C任意长度的字符串 [英] Arbitrary length string in C

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

问题描述

我需要做一些操作上的字符串(如在某些地方增加字母)。不知其大小将是什么,这取决于输入

I need to do some manipulation on a string (like adding letters at certain places). I don’t know what its size will be, it depends on the input.

我如何定义没有指定其大小的字符串?我希望它能够适应我把里面的任何内容。

How can I define a string without specifying its size? I want it to adapt to any contents I put inside.

为什么我不能只传递一个空的的char * 来的方法?编译器不允许。

And why can’t I just pass an empty char * to a method? The compiler doesn’t allow that.

据答案,我知道我不能创造无限大小的字符串。但如当我试图创建字符ARR [100] 和我的字符串的长度只有8个字符,当我打印改编我得到了更多的字母,然后我想要的。他们中的一些人不是字符串的一部分,甚至。这怎么可能?

According to the answers, I understand that I can’t create an unlimited size string. But e.g. when I tried to create char arr[100] and my string’s length was only 8 characters, when I printed arr I got more letters then I wanted. Some of them weren’t even part of the string. How is that possible?

推荐答案

您不能定义与未知大小的字符串。但是,在需要的时候,你可以使字符串大。所有的字符串和I / O功能,需要获得一个已分配的字符串的工作,所以你必须与配置,发挥一点点,注意不要超过分配容量。

Short answer

You cannot define a string with unknown size. But you may make the string larger and larger when needed. All string and I/O functions need to get an already allocated string to work with, so you have to play with allocation a little bit and be careful not to exceed the allocated capacity.

然而,当你对字符串长度恒定的上限,只分配了一个长字符串,你会是安全的。调整大小比较复杂所以要避免它,如果你不需要它。

However, when you have a constant upper bound on string length, just allocate string of that length and you’ll be safe. Resizing is more complex so avoid it if you don’t need it.

字符串是字符数组的一部分,由终止\\ 0 。字符串函数停止读取阵列时,他们遇到的第一个 \\ 0 字符。请记住, 的strlen 返回字符数的第一 \\ 0 ,所以即使数组中的第 \\ 0 ,字符串长度严格小于底层数组长度短。特别是在这种情况下,数组的长度的strlen + 1 。分配的字符串时,这是重要的;一直分配空间终止 \\ 0

String is a part of char array, terminated by \0. String functions stop reading the array when they encounter the first \0 character. Remember that strlen returns the number of characters before the first \0, so even if the array ends immediately after the first \0, string length is strictly less than the underlying array length. Specifically in that case array length is strlen + 1. This is important when allocating a string; always allocate space for the terminating \0!

例如

char w[7] = "Hello";

是相同的

char w[7] = {'H', 'e', 'l', 'l', 'o', '\0', '\0'};

当作为字符串使用时,第一个 \\ 0 是字符串,任何它被忽略后(而不是由字符串函数读取)结束。即使你通过一个可打印字符改写的例子字符数组的最后一个元素(如 W [6] =''; 导致有一个 { 'H','E','L','L','O','\\ 0',} ),看跌期权(W)!; 将打印您好(不是您好!或任何东西一样)。

When used as a string, the first \0 is end of string and anything after it is ignored (not read by string functions). Even if you rewrite the last element of the example char array by a printable character (e.g. w[6] = '!'; resulting in having {'H', 'e', 'l', 'l', 'o', '\0', '!'}), puts(w); will print Hello (not Hello! or anything alike).

在使用字符串与字符数组玩,一定要始终包括 \\ 0在其结束的后阵,否则可能未分配的内存由字符串函数导致读段错误

When playing with strings as with char arrays, be sure to always include \0 at its end as otherwise potentially unallocated memory after the array is read by string functions which results in segfault.

正如我已经写,字符串是字符数组的一部分。每个阵列必须有一个固定的大小。您可以使用只是它的一部分(这实际上使得小),但它可以分配和分配器(的 的malloc 释放calloc )需要知道多少内存而被通缉。

As I already wrote, string is part of array of char. Each array must have a fixed size. You can use just a part of it (effectively making it smaller), but it has to be allocated and allocator (malloc, calloc) needs to know how much memory is wanted.

如果您使用数组作为大于分配的,你计划很可能与段错误崩溃在较好的情况下。如果你是非常不吉利,你的程序将不会崩溃,只会使用的内存部分阵之后,产生奇怪的结果。

If you use an array as bigger than allocated, you program is likely to crash with segfault in the better case. If you are extremely unlucky, you program will not crash and will just use the part of memory right after the array, producing weird results.

由于C99,则可以省略数组的长度规格,如果它可以从初始推断 焦W [] =你好; 是相同的结果为字符W [6] =你好; 。然而,这不会帮助你,因为你指定在编译时初始化,你需要动态改变字符串的长度在运行时。

Since C99, you can omit array length specification if it can be inferred from initializer: char w[] = "Hello"; is the same result as char w[6] = "Hello";. However this will not help you because you specify initializer at compile time and you need to dynamically change the string length at run time.

要处理无限长的字符串来,你可以创建一个固定长度的数组,每个长度过低时,分配一个新的数组长一倍,原来的内容复制到新的。 (和免费旧的。)可以使用的 的realloc 做这个工作对你来说,拥有额外的好处更高的速度时,该阵列不需要移动,可以只在就地进行更长的时间。

To handle strings of unlimited length, you can create array of a fixed length and every time its length is too low, allocate a new array twice as long and copy the original contents to the new one. (And free the old one.) You can use realloc to do this work for you, with additional benefit of higher speed when the array does not need to be moved and can just be made longer in-place.

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

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