c language_ Array (super detailed analysis, easy to master)

Posted by JesperBisgaard on Sat, 08 Jan 2022 10:41:10 +0100

Catalogue

1. Creation and initialization of array

1.1 creation of array

1.2 initialization of array

1.2.1 initialization of one-dimensional array

1.2.1 initialization of two-dimensional array

2. Some usage methods of array

2.1 calculating array size

2.2 output all contents of the array

3. Storage of array in memory

4. Array out of bounds

Mention the array name

An array is a collection of elements of the same type.

1. Creation and initialization of array

1.1 creation of array

Creation of one-dimensional array

    method: type name[size]

           Type naming[size]

    Example:   int arr[10];

             char p[5];

      double a[8];

     Note: in c The creation method shown below is not allowed in the language

     int count = 10;

     int arr2[count];

For array creation, a constant must be given in [], and variables cannot be used.

Creation of two-dimensional array

  method: type name[size][size]

         Type naming[size][size]

  Example:   int arr[10][6];

         char p[5][8];

         double a[8][3];

The two-dimensional array is composed of n one-dimensional arrays, which can be regarded as the value of the saved array (one-dimensional array).

    

1.2 initialization of array

Array initialization refers to assigning reasonable values to the contents of the array while creating the array.

1.2.1 initialization of one-dimensional array

int arr1[10] = {0,1,2};

int arr2[] = {0,1,2,3};

int arr3[5] = {0,1,2,3,4};

The number of elements of the array can be determined according to the initialization content. For example, when arr2 is given four values during initialization, the compiler will automatically recognize the number of initialization of arr2. Therefore, the array can be initialized without specifying the determined size of the array.

char arr4[3] = {'a',98, 'c'};

char arr5[] = {'a','b','c'};

char arr [] is an array of character types, so you need to bring '' to the value you want to assign during initialization. If not, ASCII code is saved, such as 98 in arr4, which represents b. Let's run and see.

Attention! Here's the point! Why does abc continue to output a period of garbled code after outputting it?

This is because after outputting abc, the compiler will continue to recognize the memory behind the array until '\ 0' is found.

Let's modify the code and have a look.

There is no garbled code after this operation!

  char arr6[] = "abc";

What is the size of arr6? It's 4!

We see that "\ 0" will be automatically added to the last side when initializing with "", so the size of arr6 is 4!

1.2.1 initialization of two-dimensional array

int arr[3][4] = {1,2,3,4};

//arr[0][0]=1;arr[0][1]=2;arr[0][2]=3;arr[0][3]=4;arr[1][0]=···arr[2][4]=0;



int arr[3][4] = {{1,2},{4,5}};

//arr[0][0]=1;arr[0][1]=2;arr[0][2]=0;arr[0][3]=0;

//arr[1][0]=4;arr[1][1]=5;arr[1][2]=0;arr[1][3]=0;

//arr[2][0]=0;arr[2][1]=0;arr[2][2]=0;arr[2][3]=0;



int arr[][4] = {{2,3},{4,5}};

It is recommended to use curly braces when initializing binary arrays.

For example, arr[3][4] can be understood as an array of three rows and four columns, or as three one-dimensional arrays (each one-dimensional array has four values)

During initialization, rows can be omitted and columns cannot be.

2. Some usage methods of array

Arrays are accessed using subscripts that start at 0.

2.1 calculating array size

Idea: the number of bytes occupied by the whole array divided by the number of bytes occupied by any element in the array is the size of the array.

The sizeof() function does this.

Example:

//One dimensional array

#include <stdio.h>

int main()
{
  int arr[5] = {0};
  int size = sizeof(arr)/sizeof(arr[0]);//Specific implementation code
  return 0;
}

//Two dimensional array

#include <stdio.h>

int main()
{
  int arr[5][2] = {0};
  int size = sizeof(arr)/sizeof(arr[0][0]);//Specific implementation code
  return 0;
}

2.2 output all contents of the array

Idea: output one by one through the loop method. One dimensional array needs a single loop, and two-dimensional array needs a double loop.

Examples of one-dimensional arrays:

#include <stdio.h>

int main()
{
  int arr[10] = {1};
  for(i=0; i<10; ++i)
  {
  printf("%d ", arr[i]);
  }
  return 0;
}

Examples of two-dimensional arrays:

#include <stdio.h>

int main()
{
  int arr[3][4] = {0};
  int i = 0;
  for(i=0; i<3; i++)
{
    int j = 0;
    for(j=0; j<4; j++)
   {
      printf("%d ", arr[i][j]);
   }
}
return 0;
}

3. Storage of array in memory

We use the following code to observe the storage of one-dimensional arrays in memory.

#include <stdio.h> 

int main()

{
  int arr[3];
  int i = 0;
  for (i = 0; i < 3; i++)
  {
    printf("arr[%d]address--->%p\n", i, &arr[i]);
  }
  return 0;
}

Carefully observe the output results. The addresses in memory from arr[0] to arr[1], and from arr[1] to arr[2] are incremented by 4. This is because the ARR array we defined is of int type, and each element accounts for 4 bytes. Therefore, it can be said that arr[0] to arr[1] to arr[2] are incremented continuously, so the addresses of elements are also incremented regularly with the increase of array subscripts.

It can be concluded that arrays are stored continuously in memory.

We use the following code to observe the storage of two-dimensional arrays in memory.

#include <stdio.h>

int main()
{
	int arr[3][3];
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 4; j++)
		{
			printf("arr[%d][%d]address--->%p\n", i, j, &arr[i][j]);
		}
	}
	return 0;
}

Through the results, we can analyze that in fact, two-dimensional arrays are linear, continuous and increasing like one-dimensional arrays.

4. Array out of bounds

Take the following code as an example:

#include <stdio.h>

int main()
{
	int arr[3];
	printf("%d", arr[100]);
	return 0;
}

The output arr[100] has far exceeded the arr[3] we created. Obviously, it has crossed the boundary, and our compiler has not reported an error and runs successfully. This requires us to pay great attention to whether the array has crossed the boundary in our usual programming.

Mention the array name

What does the array name represent? The array name represents the address of the first element of the array (with the exception of arr and sizeof (ARR)).

Let's verify it with a piece of code.

If you feel that reading this article will help you a little, please leave your praise 👍 All right, thank you!  

Topics: C C++ Back-end WPF