{0, 1, 2, 3}, / * I don't know how to make a donation for the item 0 * /
{4, 5, 6, 7}, / * I don't want to give a blessing to the family that has 1 * /
{8, 9, 10, 11} / * I don't want to give a blessing to the other party 2 * /
};
Brackets, which indicate values are optional. The following initialization is equivalent to the above example:
int a [3] [4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Access elements of two-dimensional arrays in C ++
Two-dimensional array elements are accessed using indexes, eg row index and column index. For example:
int val = a [2] [3];
The above command will access the fourth element from the 3rd row of the array. You can check it again in the diagram above. Now that we look at the example below, we used nested loops to handle a two-dimensional array:
chứa
using namespace std;
int main ()
{
// bring it back with 5 rows and 2 cots.
int a [5] [2] = {{0,0}, {1,2}, {2,4}, {3,6}, {4,8}};
The program of the experts in the field
for (int i = 0; i <5; i ++)
for (int j = 0; j <2; j ++)
{
cout << "Family member a [" << i << "] [" << j << "] la:";
cout << a [i] [j] << endl;
}
return 0;
}
Running the above C ++ program will produce the following results:
As the above explained, you can have arrays with any number of dimensions, but most of the arrays you create will be one or two dimensions.
You may not understand this chapter until you have read the chapter about Cursor in C ++.
So suppose that you have a little understanding of pointers in C ++, so let's start: An array name is a constant pointer to the first element of the array. Therefore, in the declaration:
double phithuebao [ 50 ];
phithuebao is a pointer to & phithuebao [0], which is the address of the first element of the phithuebao array. Therefore, the following program segment assigns the address of the first element of phithuebao :
double * p ; double phithuebao [ 10 ]; p = phithuebao ;
Using the array name as the constant pointer is valid, and vice versa. Therefore, * (phithuebao + 4) is the official way to access data at phithuebao [4].
Once you save the address of the first element in p, you can access array elements using * p, * (p + 1), * (p + 2), . Below is the wallet example to refer to all the concepts mentioned above:
#include using namespace std ; int main () { // mang sau co 5 phan tu. double phithuebao [ 5 ] = { 1000.0 , 2.0 , 3.4 , 17.0 , 50.0 }; double * p ; p = phithuebao ; // hien thi gia tri cac phan tu trong mang cout << "Hien thi gia tri mang boi su dung con tro!" << endl ; for ( int i = 0 ; i < 5 ; i ++ ) { cout << "Gia tri cua *(p + " << i << ") la: " ; cout << *( p + i ) << endl ; } cout << "Hien thi gia tri mang boi su dung phithuebao lam dia chi!" << endl ; for ( int i = 0 ; i < 5 ; i ++ ) { cout << "*Gia tri cua (phithuebao + " << i << ") la: " ; cout << *( phithuebao + i ) << endl ; } return 0 ; }
Running the above C ++ program will produce the following results:
In the above example, p is a pointer to double, which means it can store the address of a variable of type double. Once we have the address in p, then * p will provide the available value at the address stored in p, as we have shown in the above example.
C ++ does not allow you to return an entire array as a parameter to a function. However, you can return a pointer to an array by specifying the array name that is not an index. You will learn about pointers in the next chapter, so you can skip this chapter until you understand the concept of Cursor in C ++.
If you want to return a one-dimensional array from a function, you will have to declare a function that returns a pointer as in the following example:
int * tenHam () { . . . }
The second point to remember is that C ++ does not support returning the address of the local variable to outside the function, so you will have to define the local variable as a Static variable.
Now suppose the following function will generate 10 random numbers and return them using an array and call this function as follows:
#include #include #include /* thu vien cho ham srand, rand */ using namespace std ; // phan dinh nghia cua ham de tao va tra ve cac so ngau nhien. int * soNgauNhien ( ) { static int r [ 10 ]; srand ( ( unsigned ) time ( NULL ) ); for ( int i = 0 ; i < 10 ; ++ i ) { r [ i ] = rand (); cout << r [ i ] << endl ; } return r ; } // ham main de goi phan dinh nghia ham tren. int main () { // mot con tro tro toi mot so nguyen. int * p ; p = soNgauNhien (); for ( int i = 0 ; i < 10 ; i ++ ) { cout << "Gia tri cua *(p + " << i << ") la: " ; cout << *( p + i ) << endl ; } return 0 ; }
Running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous post: Number in C ++
Next lesson: String (String) in C / C ++