c语言中数组越界怎么办_如果我们使用C语言数组中的越界索引怎么办?

c语言中数组越界怎么办

Let’s understand first, what is index out of bounds?

首先让我们了解一下什么是索引超出范围?

Let suppose you have an array with 5 elements then the array indexing will be from 0 to 4 i.e. we can access elements from index 0 to 4.

假设您有一个包含5个元素的数组,那么数组索引将从0到4,即我们可以访问从索引0到4的元素。

But, if we use index which is greater than 4, it will be called index out of bounds.

但是,如果我们使用大于4的索引,它将被称为索引超出范围。

If we use an array index that is out of bounds, then the compiler will probably compile and even run. But, there is no guarantee to get the correct result. Result may unpredictable and it will start causing many problems that will be hard to find.

如果我们使用的数组索引超出范围,则编译器可能会编译甚至运行。 但是,不能保证获得正确的结果。 结果可能无法预测,并且将开始引起许多难以发现的问题。

Therefore, you must be careful while using array indexing.

因此, 在使用数组索引时必须小心

Consider the example:

考虑示例:

#include <stdio.h>
int main(void) 
{
int arr[5];
int  i;
arr[0] = 10;  //valid
arr[1] = 20;  //valid
arr[2] = 30;  //valid
arr[3] = 40;  //valid
arr[4] = 50;  //valid
arr[5] = 60;  //invalid (out of bounds index)
//printing all elements
for( i=0; i<6; i++ )
printf("arr[%d]: %d\n",i,arr[i]);
return 0;
}

Output

输出量

    arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50
arr[5]: 11035

Explanation:

说明:

In the program, array size is 5, so array indexing will be from arr[0] to arr[4]. But, Here I assigned value 60 to arr[5] (arr[5] index is out of bounds array index).

在程序中,数组大小为5,因此数组索引将从arr [0]到arr [4] 。 但是,在这里,我为arr [5]分配了值60( arr [5] 索引超出范围数组索引 )。

Program compiled and executed successfully, but while printing the value, value of arr[5] is unpredictable/garbage. I assigned 60 in it and the result is 11035 (which can be anything).

程序已成功编译并执行,但是在打印该值时, arr [5]的值是不可预测的/垃圾。 我在其中分配了60,结果是11035(可以是任何值)。

翻译自: https://www.includehelp.com/c/out-of-bounds-index-in-an-array-in-c.aspx

c语言中数组越界怎么办

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379766.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

FreeRTOS任务基础知识

任务特性 在RTOS中&#xff0c;一个实时应用可以作为一个独立的任务&#xff0c;支持抢占&#xff0c;支持优先级&#xff0c;每个任务都有自己的堆栈&#xff0c;当任务切换时将上下文环境保存在堆栈中&#xff0c;再次调用任务时&#xff0c;取出上下文信息&#xff0c;继续…

测试Rockey 4 Smart加密锁的C语言代码

测试Rockey 4 Smart加密锁的C语言代码 // win32Console_dog_test.cpp : Defines the entry point for the console application. /// // //测试Rockey 4 Smart加密锁的C语言代码 // /// #include "stdafx.h" #include <conio.h> #include "time.h" #…

C——任意一个偶数分解两个素数

题目&#xff1a;一个偶数总能表示为两个素数之和 以上实例运行输出结果为&#xff1a; 请输入一个偶数: 4 偶数4可以分解成1和3两个素数的和 #include <stdio.h> #include <stdlib.h> int Isprimer(int n); int main() {int n,i;do{printf("请输入一个偶数&…

c#委托调用另一窗口函数_在C#中使用委托调用成员函数

c#委托调用另一窗口函数Prerequisite: Delegates in C# 先决条件&#xff1a; C&#xff03;中的代表 We can also call a member function of a class using delegates. It is similar to static function calls, here we have to pass member function using an object on t…

Java版AVG游戏开发入门[0]——游戏模式转换中的事件交互

Java版AVG游戏开发入门[0]——游戏模式转换中的事件交互 示例程序下载地址&#xff1a;http://download.csdn.net/source/999273&#xff08;源码在jar内&#xff09; AVG&#xff0c;即Adventure Game&#xff0c;可以直译为[冒险游戏]。但是通常情况下我们说AVG是指[文字冒险…

FreeRTOS任务创建和删除

任务创建和删除的API函数 xTaskCreate()&#xff1a;使用动态方法创建一个任务xTaskCreateStatic()&#xff1a;使用静态方法创建一个任务xTaskCreateRestricated()&#xff1a;创建一个使用MPU进行限制的任务&#xff0c;相关内存使用动态内存分配vTaskDelete()&#xff1a;删…

Delphi 调试

调试&#xff1a;F9执行F8逐过程单步调试F7逐语句单步调试转载于:https://www.cnblogs.com/JackShao/archive/2012/04/30/2476931.html

1.创建单项链表

# include <stdio.h> # include <malloc.h> # include <stdlib.h>typedef struct Node{int data;//数据域struct Node *pNext;//指针域}NODE, *PNODE; //NODE等价于struct Node //PNOD等价于struct Node * //函数声明PNODE create_list(void); void traverse…

python 日本就业_日本的绘图标志 Python中的图像处理

python 日本就业Read basics of the drawing/image processing in python: Drawing flag of Thailand 阅读python中绘图/图像处理的基础知识&#xff1a; 泰国的绘图标志 The national flag of Japan is a rectangular white banner bearing a crimson-red disc at its center…

[windows phone 7 ]查看已安装程序GUID

首先介绍下wp7RootToolsSDK,这个功能相当强大&#xff0c;适合研究wp7高级功能。 它支持File&#xff0c;Register操作&#xff0c;比之前的COM调用要简单&#xff0c;方便。 功能:查看已安装程序的guid 开发心得: 用的是mozart,rom多&#xff0c;刷机吧&#xff0c;最麻烦的是…

FreeRTOS任务挂起和恢复

任务挂起&#xff1a;暂停某个任务的执行 任务恢复&#xff1a;让暂停的任务继续执行 通过任务挂起和恢复&#xff0c;可以达到让任务停止一段时间后重新运行。 相关API函数&#xff1a; vTaskSuspend void vTaskSuspend( TaskHandle_t xTaskToSuspend );xTaskToSuspend &am…

向oracle存储过程中传参值出现乱码

在页面中加入<meta http-equiv"Content-Type" content"text ml;charsetUTF-8"/>就可以解决这一问题 适用情况&#xff1a; 1.中文 2.特殊符号 转载于:https://www.cnblogs.com/GoalRyan/archive/2009/02/16/1391348.html

Scala程序将多行字符串转换为数组

Scala | 多行字符串到数组 (Scala | Multiline strings to an array) Scala programming language is employed in working with data logs and their manipulation. Data logs are entered into the code as a single string which might contain multiple lines of code and …

SQL 异常处理 Begin try end try begin catch end catch--转

SQL 异常处理 Begin try end try begin catch end catch 总结了一下错误捕捉方法:try catch ,error, raiserror 这是在数据库转换的时候用的的异常处理, Begin TryInsert into SDT.dbo.DYEmpLostTM(LogDate,ProdGroup,ShiftCode,EmployeeNo,MONo,OpNo,OTFlag,LostTypeID,OffStd…

FreeRTOS中断配置与临界段

Cortex-M中断 中断是指计算机运行过程中&#xff0c;出现某些意外情况需主机干预时&#xff0c;机器能自动停止正在运行的程序并转入处理新情况的程序&#xff08;中断服务程序&#xff09;&#xff0c;处理完毕后又返回原被暂停的程序继续运行。Cortex-M内核的MCU提供了一个用…

vector向量容器

一、vector向量容器 简介&#xff1a; Vector向量容器可以简单的理解为一个数组&#xff0c;它的下标也是从0开始的&#xff0c;使用时可以不用确定大小&#xff0c;但是它可以对于元素的插入和删除&#xff0c;可以进行动态调整所占用的内存空间&#xff0c;它里面有很多系统…

netsh(二)

netsh 来自微软的网络管理看家法宝很多时候&#xff0c;我们可能需要在不同的网络中工作&#xff0c;一遍又一遍地重复修改IP地址是一件比较麻烦的事。另外&#xff0c;系统崩溃了&#xff0c;重新配置网卡等相关参数也比较烦人&#xff08;尤其是无线网卡&#xff09;。事实上…

java uuid静态方法_Java UUID getLeastSignificantBits()方法与示例

java uuid静态方法UUID类getLeastSignificantBits()方法 (UUID Class getLeastSignificantBits() method) getLeastSignificantBits() method is available in java.util package. getLeastSignificantBits()方法在java.util包中可用。 getLeastSignificantBits() method is us…

Google C2Dm相关文章

Android C2DM学习——云端推送&#xff1a;http://blog.csdn.net/ichliebephone/article/details/6591071 Android C2DM学习——客户端代码开发&#xff1a;http://blog.csdn.net/ichliebephone/article/details/6626864 Android C2DM学习——服务器端代码开发&#xff1a;http…

FreeRTOS的列表和列表项

列表和列表项 列表 列表是FreeRTOS中的一个数据结构&#xff0c;概念上和链表有点类型&#xff0c;是一个循环双向链表&#xff0c;列表被用来跟踪FreeRTOS中的任务。列表的类型是List_T&#xff0c;具体定义如下&#xff1a; typedef struct xLIST {listFIRST_LIST_INTEGRI…