请问c具有一个字符串类型? [英] Does C have a string type?

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

问题描述

我最近用C开始编程,从Java和Python的到来。现在,在我的书,我已经注意到了,做一个Hello World程序,语法是这样的:

I have recently started programming in C, coming from Java and Python. Now, in my book I have noticed that to make a "Hello World" program, the syntax is something like this:

char message[10]
strcpy(message, "Hello, world!")
printf("%s\n", message);

现在,这个例子是使用字符数组,我想知道 - 发生了什么条件?为什么我不能简单地用其中的一个?也许有不同的方式来做到这一点?

Now, this example is using a char array and I wondered - what happened to strings? Why can't I simply use one of those? Maybe there is a different way to do this?

推荐答案

C没有,从来没有有过本地字符串类型。按照惯例,语言使用字符阵列用一个空字符,即以'\\ 0'终止。在语言的标准库函数和宏为空终止字符数组,例如,的 strlen的遍历字符的数组,直到遇到一个'\\ 0'字符和<一个HREF =htt​​p://pubs.opengroup.org/onlinepubs/009695399/functions/strcpy.html相对=nofollow> strcpy的从源字符串拷贝,直到遇到一个 \\ 0'

C does not and never has had a native string type. By convention, the language uses arrays of char terminated with a null char, i.e., with '\0'. Functions and macros in the language's standard libraries provide support for the null-terminated character arrays, e.g., strlen iterates over an array of char until it encounters a '\0' character and strcpy copies from the source string until it encounters a '\0'.

在C中的使用空值终止字符串反映出下意欲是只有一点点更多的层次比很高汇编语言。零结尾的字符串已经直接支持的PDP-10和PDP汇编语言-11 。

The use of null-terminated strings in C reflects the fact that C was intended to be only a little more high-level than assembly language. Zero-terminated strings were already directly supported at that time in assembly language for the PDP-10 and PDP-11.

值得注意的是C字符串的这一特性导致了相当多的讨厌的缓冲区溢出漏洞,其中包括严重的安全漏洞。例如,如果你忘记空 - 终止作为源参数传递的字符串为的strcpy ,该功能将继续从任何抄袭连续字节恰好是在内存中过去源字符串的结束,直到它恰好遇到一个 0 ,可能覆盖任何有价值的信息如下内存中的目标字符串的位置。

It is worth noting that this property of C strings leads to quite a few nasty buffer overrun bugs, including serious security flaws. For example, if you forget to null-terminate a character string passed as the source argument to strcpy, the function will keep copying sequential bytes from whatever happens to be in memory past the end of the source string until it happens to encounter a 0, potentially overwriting whatever valuable information follows the destination string's location in memory.

在您的code例如,字符串你好,世界!将汇编成字符的一个14字节的多头排列。第一个13个字节将持有的字母,逗号,空格和感叹号和最后一个字节将持有空终止符'\\ 0',自动为你添加编译器。如果你要访问数组的最后一个元素,你会发现它等于 0 。例如:

In your code example, the string literal "Hello, world!" will be compiled into a 14-byte long array of char. The first 13 bytes will hold the letters, comma, space, and exclamation mark and the final byte will hold the null-terminator character '\0', automatically added for you by the compiler. If you were to access the array's last element, you would find it equal to 0. E.g.:

const char foo[] = "Hello, world!";
assert(foo[12] == '!');
assert(foo[13] == '\0');

不过,在你的榜样,的消息只有10字节长。 的strcpy 会写所有14个字节,包括空终止符,到存储器中,起始的消息的地址。第一个10个字节将被写入到的消息,其余四个字节在栈上分配的内存将简单地写在堆栈的末尾。写这四个额外字节到堆栈的后果是很难predict在这种情况下,(在这个简单的例子,它可能不会伤害的事情),但在现实世界中的code这通常会导致数据损坏或内存访问冲突的错误。

However, in your example, message is only 10 bytes long. strcpy is going to write all 14 bytes, including the null-terminator, into memory starting at the address of message. The first 10 bytes will be written into the memory allocated on the stack for message and the remaining four bytes will simply be written on to the end of the stack. The consequence of writing those four extra bytes onto the stack is hard to predict in this case (in this simple example, it might not hurt a thing), but in real-world code it usually leads to corrupted data or memory access violation errors.

这篇关于请问c具有一个字符串类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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