22 array pointer and pointer array

catalogue

problem

  • Are the following statements legal?

    int array[5];
    int matrix[3][3];
    int* pa = array;
    int* pm = matrix;
    

Array represents the address of the first element of the array. What does matrix represent?

The address values of array and & array are the same, but their meanings are different. So do they represent the same type?

1 array type

  • Arrays in C language have their own specific types
  • The type of array is determined by the element type and the size of the array
    • For example, the type of int array[5] is int[5]

2. Define array type

  • Renaming array types through typedef in C language

    typedef type(name)[size];

    The element type of the array type is: type, the array size of the array type is: size, and the array type is renamed name

  • Examples

    • Array type

      typedef int(AINT5)[5];
      typedef float(AFLOAT10)[10];
      
    • Use the renamed array type to define the array

      AINT5 iArray;
      AFLOAT10 fArray;
      

3 array pointer

  • Similar to defining an int type, you can define an int type pointer. You can use the array type defined above to define the array pointer

  • Array pointers are used to point to an array (not the first element of the array)

  • The array name is the starting address of the first element of the array, but it is not the starting address of the array

  • The starting address of the array can be obtained by applying the address fetcher & to the array name

  • Define array pointers through array types: ArrayType* pointer; You can also directly define: type(*pointer)[n];

    ArrayType is the renamed array type, pointer is the array pointer variable name, type is the element type of the array pointed to, and n is the size of the array pointed to

  • Examples of array types and array pointers

    • Demo

      #include <stdio.h>
      
      //Rename array type
      typedef int(AINT5)[5];
      typedef float(AFLOAT10)[10];
      typedef char(ACHAR9)[9];
      
      int main()
      {
          AINT5 a1;  //Use the renamed array type to define an array with 5 elements in total, and each element type is int
          float fArray[10];  //Directly define an array with 10 elements, and each element type is float
          AFLOAT10* pf = &fArray;  //Use the array type to define an array pointer. The type pointed to is AFLOAT10, that is, the pf pointer points to an array. The array has 10 elements, each of which is of type float, and then initialized to the address of frarray
          ACHAR9 cArray;  //Use the renamed array type to define an array with 9 elements, and each element type is char
      
          char(*pc)[9] = &cArray;  //Directly define an array pointer pc, which points to an array. The array has 9 elements, each of which is of type char, and then initialize it to the address of crarray
          char(*pcw)[4] = cArray;  //error: directly define an array pointer pcw. The pcw pointer points to an array. The array has four elements. Each element type is char, and then initialize it to the address of the first element of the array (type is char *). It is illegal here!
          
          int i = 0;
          
          printf("%d, %d\n", sizeof(AINT5), sizeof(a1));  //20=4*5
          
          for(i=0; i<10; i++){
              (*pf)[i] = i;  // => fArray[i] = i;
          }
          
          for(i=0; i<10; i++){
              printf("%f\n", fArray[i]);
          }
          
          printf("%p, %p, %p\n", &cArray, pc+1, pcw+1);  //pc+1 = (unsigned)pc+sizeof(*pc) = (unsigned)pc+9, pcw+1 = (unsigned)pcw+sizeof(*pc) = (unsigned)pcw+4
      
          return 0;
      }
      
    • compile

      test.c: In function 'main':
      test.c:16 warning: initialization from incompatible(Mismatch) pointer type
      
    • function

      20,20
      0.000000
      1.000000
      2.000000
      3.000000
      4.000000
      5.000000
      6.000000
      7.000000
      8.000000
      9.000000
      0xbf8b43d3,0xbf8b43dc,0xbf8b43d7
      

4 pointer array

  • Pointer array is an ordinary array

  • Each element in the pointer array is a pointer

  • Definition of pointer array: type* pArray[n];

    Type * is the type of each element in the array, pArray is the array name, and n is the array size

  • Application of pointer array

    • Demo

      #include <stdio.h>
      #include <string.h>
      
      //Define a macro: calculate the size of the array
      #define DIM(a) (sizeof(a)/sizeof(*a))
      
      //Table is a pointer array, and each element in the array is const char *. When the array is used as a formal parameter, it will degenerate into a pointer, that is = > const char * * table
      int lookup_keyword(const char* key, const char* table[], const int size)
      {
          int ret = -1;
          
          int i = 0;
          
          for(i=0; i<size; i++){
              //Compare whether the string key is equal to each string in the table array
              if( strcmp(key, table[i]) == 0 ){
                  ret = i;
                  break;
              }
          }
          
          return ret;
      }
      
      int main()
      {
          const char* keyword[] = {
                  "do",
                  "for",
                  "if",
                  "register",
                  "return",
                  "switch",
                  "while",
                  "case",
                  "static"
          };
          
          printf("%d\n", lookup_keyword("return", keyword, DIM(keyword)));
          printf("%d\n", lookup_keyword("main", keyword, DIM(keyword)));
      
          return 0;
      }
      
    • Compile and run

      4
      -1

Tags: C

Posted by Toby on Tue, 17 May 2022 11:21:24 +0300