Data structure life from scratch - array and pointer basis (including personal in-depth understanding)

Posted by Morbid on Sun, 30 Jan 2022 16:22:55 +0100

Freshmen enter the data structure. Please give advice on mistakes and omissions and make progress with you.

Arrays and pointers

  • What is an array

    • A contiguous space (clearly telling the compiler how many blocks of space there are) is used for the same type of data

    • The name of the array is for the user to see. In the actual memory, the address is used to find it

    • Access elements through the array subscript, which starts from 0

    • Arrays are stored sequentially

    • The array name is the address of the first element

  • One dimensional array

    int array1[3]={1,2,3};//initiate static
    int array2[3];//Dynamic initialization is used to traverse input and output
    for(int i=0;i<3;i++){
        array2[i]=1;
    }
    for(int j=0;j<3;j++){
        printf("%d\n",array[j]);
    }
    int array3[3]={[1]=2,3};//Specify initialization
    • The assignment will be delayed during the specified initialization, and no error will be reported if it exceeds the limit

    • When individual elements are initialized, other elements are 0 by default

  • Two dimensional array

    • The actual memory is not a matrix, but a nesting of one-dimensional arrays

    • array01[3] stores the corresponding addresses of three (one-dimensional array) sequences, which are essentially pointers. It stores the addresses pointing to three arrays (pointing to three addresses)

      int array01[3][3] = {//Static initialization of two-dimensional array
              {1,2,3},
              {7,8,9},
              {4,5,6}
          };
      for (int i = 0; i < 3; i++) {//Two dimensional traversal
              for (int j = 0; j < 3; j++) {
                  printf("%d\n", array01[i][j]);

  • Pointer

    • The pointer points to a variable (concrete space), that is, the address where the variable (concrete space) is stored in the pointer variable

    • The address is compiled in bytes. The address is hexadecimal

    • The pointer needs to be initialized, but it is not a simple assignment, but points to a specific space

    • Wild pointer: there is a pointer to space, but it is random

    • NULL pointer: refers to NULL, which is used for judgment. One more step judgment is for the robustness of the program, such as data boundary problems

      //Error demonstration
      int *p;
      *p=5;//Because this 5 does not know where it is stored, it may modify the internal data, so it is regarded as illegal access
      //Correct demonstration
      int a;//There is specific space
      int* p1=&a;
      *p1=5;

  • Relationship between array and pointer

    • In array[3]={2,4,6}; There are two ways to find 4 this element: one is through the array subscript array[1], the other is * (p+1), which is essentially through the index

    • Understand the function of pointer type (why pointer should have type)

      int array={1,3,5};
      int *p=array;
      printf("%d\n",*p+1);//The result is 2
      printf("%d\n",(*p+1));//The result is 2
      printf("%d\n",(*p)+1);//The result is 2
      printf("%d\n",*(p+1));//The result is 3
      //The essential difference lies in the priority relationship between * and +
      //But why can p+1 point to the next grid? The reason is that the pointer has a type
      //Therefore, the function of pointer type is to facilitate movement, that is, to find the same type of data after + 1 (for example, 4 bytes for int type and 8 bytes for double type)
      //So in this example, address + 1 is actually adding an int type storage unit
      //So array + 4 < = > & array [2]

    • Arrays are pointers (understood through two-dimensional arrays)

      int array[3][3] = {//Two dimensional array
              {1,2,3},//Why does array[3] store the corresponding addresses of three (one-dimensional array) sequences?
              {7,8,9},
              {4,5,6}
          };
      int(*p)[3];//Another expression of two-dimensional array
      p=array;
      • Using dimensionality reduction to understand

        1. Pointer to normal variable

          int a=5; 
          int*p=&a;

        p points to the concrete space a

        That is, the space of p stores the address of space a

        P = & A

        When * p, find the value of the address placed in p (find what is in this space)

        1. Pointer to one-dimensional array

          int array[3]={4,5,6};
          int* p=array;

          The first element address is determined by the array name

          p points to 4, which is a value (space) of type int

          That is, when * p, the first element 4 can be found. When array[0], the first element 4 can also be found

          So * p is equivalent to array[0]

          So * p+1 is equivalent to array[1]

        2. The pointer points to a two-dimensional array

          int array[2][2] = {
                  {5,6},
                  {7,8}
              };
          int (*p)[2];
          p=array;
          • The array name is the address of the first element

          • Array name array is the address of array[0], that is, the address where array stores array[0], that is, array points to array[0], and array[0] is a one-dimensional array containing two ints, so array points to a space of two ints (in this case, array[0] is understood as an array)

          • If you can make array[0] array1, the one-dimensional array is simplified to array1[3]

          • Array name array1 is the address of array[0][0], and array name array[0] is the address of array[0][0], that is, the address where array[0] stores array[0][0], that is, array[0] points to array[0][0], and array[0][0] contains a single element of an int, so array[0] points to a space of int size (in this case, array[0] is understood as a pointer)

          • array==&array[0]
            array[0]==&array[0][0]
            **array==*array[0]==array[0][0]
            //Therefore, it can only be explained that it starts from the same address, but the pointing space size is different
            //So when + 1 moves, you get different values

          • So * p points to a space containing two ints

            printf("%d\n",**p+1);//The result is 6
            printf("%d\n",*(*p+1));//The result is 6
            printf("%d\n",**(p+1));//The result is 7 structures

Topics: C C++ data structure pointer array