字符数组的空终止 [英] Null termination of char array

查看:139
本文介绍了字符数组的空终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下情况:

#include<stdio.h>
int main()
{
    char A[5];
    scanf("%s",A);
    printf("%s",A);
}

我的问题是,如果字符 A [5] 只包含两个字符。说AB,那么 A [0] ='A' A [1] ='B' A [2] ='\\ 0'
但是,如果输入的是说,ABCDE,那么哪里是'\\ 0'在这种情况下。请问 A [5] 包含'\\ 0'
如果是,为什么?
的sizeof(A)总是返回5作为答案。然后,当数组已满,是否有'\\ 0'其中的sizeof()没有按保留一个额外的字节ŧ算什么?

My question is if char A[5] contains only two characters. Say "ab", then A[0]='a', A[1]='b' and A[2]='\0'. But if the input is say, "abcde" then where is '\0' in that case. Will A[5] contain '\0'? If yes, why? sizeof(A) will always return 5 as answer. Then when the array is full, is there an extra byte reserved for '\0' which sizeof() doesn't count?

推荐答案

如果您键入四个以上的字符,然后将多余的字符和空终止将在数组末尾之外写入,覆盖内存不属于阵列。这是一个缓冲区溢出。

If you type more than four characters then the extra characters and the null terminator will be written outside the end of the array, overwriting memory not belonging to the array. This is a buffer overflow.

C没有prevent你弄错你没有自己的记忆。这导致的 未定义行为 。您的程序可以做任何事情&MDASH;它可能会崩溃,也可能默默垃圾其他变量而造成混淆的行为,也可能是无害的,或其他任何东西。请注意,不能保证你的程序要么可靠地工作或崩溃可靠。你甚至不能依靠它会立即崩溃。

C does not prevent you from clobbering memory you don't own. This results in undefined behavior. Your program could do anything—it could crash, it could silently trash other variables and cause confusing behavior, it could be harmless, or anything else. Notice that there's no guarantee that your program will either work reliably or crash reliably. You can't even depend on it crashing immediately.

这就是为什么 scanf函数(%S)是危险的,不应该使用一个很好的例子。它不知道你的数组的大小,这意味着没有办法安全地使用它。相反,避免scanf函数和使用的东西更安全,如与fgets()

This is a great example of why scanf("%s") is dangerous and should never be used. It doesn't know about the size of your array which means there is no way to use it safely. Instead, avoid scanf and use something safer, like fgets():

与fgets()最多读取比大小减1 字符从流,并将它们存储到缓冲区由s指向。一个EOF或换行后读停止。如果一个新行被读取时,它被存储到缓冲器中。终止空字节('\\ 0')存储在缓冲区中的最后一个字符之后。

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

例如:

if (fgets(A, sizeof A, stdin) == NULL) {
    /* error reading input */
}

烦人,与fgets()将在数组末尾留下尾随换行符('\\ n')。所以,你可能还需要code将其删除。

Annoyingly, fgets() will leave a trailing newline character ('\n') at the end of the array. So you may also want code to remove it.

size_t length = strlen(A);
if (A[length - 1] == '\n') {
    A[length - 1] = '\0';
}

唉。一个简单的(但碎) scanf函数(%S)变成了7号线的怪物。而这一天的第二个教训:C是不擅长的I / O和字符串处理。这是可以做到,而且可以安全地完成,但按c踢和尖叫的全部时间。

Ugh. A simple (but broken) scanf("%s") has turned into a 7 line monstrosity. And that's the second lesson of the day: C is not good at I/O and string handling. It can be done, and it can be done safely, but C will kick and scream the whole time.

这篇关于字符数组的空终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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