C语言中的数组结尾 [英] End of array in C language

查看:31
本文介绍了C语言中的数组结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道对于字符数组,我们有 '\0' 来标识它的结尾,那么其他类型的数组呢?

We know that for character arrays we have the '\0' to identify its end, what about other types of arrays?

我认为它不是像 '\0' 这样的特殊字符,因为要声明一个包含 n 个元素的整数数组,例如您只分配 n * sizeof(int),但对于字符数组,我们考虑了 '\0'.

I presume it's not a special character such as the '\0' since to declare an integer array of n elements for example you just allocate n * sizeof(int), yet for a character array we account for the '\0'.

推荐答案

C 数组没有结束标记.

C arrays don't have an end marker.

作为程序员,您有责任跟踪数组的分配大小,以确保您不会尝试访问分配大小之外的元素.

It is your responsibility as the programmer to keep track of the allocated size of the array to make sure you don't try to access element outside the allocated size.

如果您确实访问了超出分配大小的元素,结果是未定义行为.

If you do access an element outside the allocated size, the result is undefined behaviour.

这篇关于C语言中的数组结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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