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.pswp.cn/news/379766.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379766.shtml
英文地址,請注明出處:http://en.pswp.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…