0

Array

Why array is outputting numbers that are not in the array https://sololearn.com/compiler-playground/cB6rfuP67g4A/?ref=app

11th Jul 2024, 12:09 PM
Vansh
Vansh - avatar
4 Respostas
+ 3
int arr[ 5 ][ 3 ] = { {2, 3}, {4, 5}, {4, 6} }; cout <<arr[ 3 ][ 6 ]; You created a multidimensional array having 5 rows with 3 columns each. Then you want to display value of the 7th column from the 4th row. The first issue is with the array initializer. It only provided values for 3 rows with 2 columns each. So, fix the array initializer to provide enough values accordingly (5 rows with 3 columns each). int arr[ 5 ][ 3 ] = { { 1, 2, 3 }, // row index 0 { 4, 5, 6 }, // row index 1 { 7, 8, 9 }, // row index 2 { 10, 11, 12 }, // row index 3 { 13, 14, 15 } // row index 4 }; Second, fix the index of the data to display. You should not attempt to access data beyond the array's boundaries (5 row - 3 columns each) arr[ 3 ][ 6 ] refers to the 7th column of the 4th row (remember indices starts from zero). Array <arr> was supposed to have 5 rows with 3 columns each. So you should not refer the 7th column because each row was only defined with 3 columns. C/C++ allows an attempt to access (read/write) memory beyond what the program requested from the system, some people term it "shoot yourself in the foot". But the consequences is unpredictable, program can crash, or work unintendedly from such deed. We could try best we can to avoid access to data that doesn't belong to our program :)
11th Jul 2024, 2:54 PM
Ipang
+ 4
You are actually trying to access an element which does not exist at all. Accessing an element in an array outside of its bound will not result in an error or exception instead it will return 0.
11th Jul 2024, 2:06 PM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 4
Vansh the given initial array values are incomplete, so the compiler fills in the missing values with zero. We may think of the 5x3 array as represented like this: 2,3,0 4,5,0 4,6,0 0,0,0 0,0,0 Though in memory, the values are merely a contiguous block like this: 2,3,0,4,5,0,4,6,0,0,0,0,0,0,0 The compiler translates array references into a pointer calculation. For a two-dimensional array, int a[5][3], arr[i][j] becomes *(arr + i*3 + j). So arr[3][6] is really *(arr + 3*3 + 6) or *(arr+15). Now look at the above linear memory layout. If arr points to the first element, 2, and you count 15 beyond that, the reference goes outside the array boundary by one element. It reads some other memory that is not part of the array. In rare esoteric cases this is useful, so C++ allows it, but usually it is considered a bug. Be on your guard against it.
11th Jul 2024, 3:40 PM
Brian
Brian - avatar
+ 1
These are all the values in your array. #include <iostream> using namespace std; int main() { int arr[5][3]= {{2,3},{4,5},{4,6}}; for(int i=0;i<5;i++){ for(int j=0;j<3;j++) cout<<"arr["<<i<<"]["<<j<<"] = " <<arr[i][j]<<'\n'; } } There is no arr[3][6]. There are arr[3][0], arr[3][1] and arr[3][2] what you are accessing are garbage values if you go out of the bounds of the array. It's an inherited weakness from C.
12th Jul 2024, 4:26 PM
Bob_Li
Bob_Li - avatar