C++ introductory tutorial — 13 arrays

An array is a data structure that stores a sequential collection of fixed-size elements of the same type. An array is used to store a series of data, but it is often thought of as a series of variables of the same type.

Declare array: int a[10]; // This is a declaration of 10 ints

Initialize array: int b[5]={1,2,3,4,5}; // This is an array initialization. Declaration and initialization can go together.

Use arrays: b[0],b[1]; The important thing here is that the subscripts start at 0. For example, int b [5]; It’s declaring an array. But the arrays that can be used are B [0], B [1], B [2], B [3], B [4]; If b[5] is used, it is wrong. This is called array out of bounds, and of course B [10] is array out of bounds. The program will crash.

Example:

#include <iostream>
using namespace std;


int b[5] = {10.20.30.40.50};

int main (a)
{
  int i; // Subscripts start at zero
  for(i=0; i<5; i++) { cout<<b[i]<<"";
  } 
  
  return 0;
}
Copy the code

Running results: