Array pointer and pointer array

Array pointer

  before unveiling the magic of array pointers, let's explore whether arrays have their own specific types?

1. Array type

  we know that an array is an ordered collection of variables of the same type. For example, int a[5] = {0};, The meaning is that five int variables are collected together and managed by array a. So I want to ask, what is the type of array a?

  if the reader is on the way to study C language, your answer should be int type. But what I want to say is, I'm sorry, this answer is wrong, so I'll tell you the answer next.

  first of all, let's be clear:

In C language, arrays have their own specific types.

  so what is the type of array?

The type of array is determined by the type of elements in the array and the size of the array.

    therefore, the type of array a defined above is int[5], not int.

2. Define array type

  after understanding the type of array, we can create an array in another way by defining the array type.

    in C language, you can rename the array type through the typedef keyword:

typefet type(name)[size];
  • int is the data type of the element in the array
  • Name is the name of the array type to be created
  • Size is the size of the array to be created

Example:

  • Define array type
// Define the data type of int and array size 5 in the array as AINT5

// It is defined that the data type with element type of float and array size of 10 in the array is AFLOAT10
typedef float(AFLOAT10)[10];
  • Declaration array
// Declare an array with data type AINT5 and array name iArray
AINT5 iArray;
// Declare an array with data type AFLOAT10 and array name fArray
AFLOAT10 fArray;

  now you should understand how to create array types. But there is another problem. For example, the int type accounts for 4 bytes and the char type accounts for 1 byte. What is the size of the array type?

  the following is an example:

typedef int(AINT5)[5];
  • The AINT5 array type says that the array elements contained in the AINT5 array type are of type int, containing a total of 5 elements
  • Because each int type occupies 4 bytes of memory space
  • Therefore, the memory space occupied by AINT5 is 20 bytes

   similarly, a calculation formula can be derived from the above:

sizeof(TypeArray) = sizeof(type)·size
  • TypeArray is an array type
  • Type is the type of the element in the array type
  • size is the number of array elements contained in the array type

   then, the memory space occupied by any kind of array type can be calculated through this formula. At the same time, the space occupied by the array created by an array type is the space occupied by the array type.

  so far, we should be in charge of data types. Let's start with the real topic array pointer.

3. Array pointer

  we talked a lot about the essence of array types and the creation of array types. Then you may think that the array type is not magical. Is it just another way to declare the array? What I want to say is that with array types, we can define array pointers. Think about it.

  first, let's recall the syntax structure of pointer declaration:

(type)* name = NULL;
  • Type is the type of the declared pointer
  • Name is the name of this pointer

  through the syntax structure of pointer declaration, we can define int, float, char and other types of pointers. So, with array types, it's easy to create array pointers.

(1) Array pointers can be defined for array types:

ArrayType* pointer;
  • ArrayType is an array type
  • Pointer is the name of the array pointer

(2) Can be defined directly by data type:

type(*pointer)[n];
  • Pointer is the array pointer variable name
  • Type is the type of the array pointed to
  • n is the number of elements of the array

    up to now, we have understood how to create array pointers. Let's deepen our impression through a demo.

#include <stdio.h>

// Rename the array whose array element is int and the number of array elements is 5 to AINT5
typedef int(AINT5)[5];
// Rename the array with flaot and 10 elements as AFLOAT10
typedef float[AFLOAT10][10];
// Rename the array whose array element is char and the number of array elements is 9 to ACHAR9
typedef char[ACHAR9][9];

int main()
{
	// Declare an array with array type AINT5 and array name iArray
	AINT5 iArray;
	// Declare an array with the array element type of float, the number of array elements of 10 and the array name of fArray
	float fArray[10];
	// Declare an array pointer of type AFLOAT10, name pfArray, and value the address of array fArray
	AFLOAT10* pfArray = &fArray;
	// Declare an array with array type ACHAR9 and array name crarray
	ACHAR9 cArray;
	
	// Declare an array pointer with type char, number of elements 9, name pc and value cArray
	char(*pc)[9] = &cArray;

	/*
	   First, arcarray is the address of the first element of the array. Its type is char*
	   char(*pcw)[4] Is an array pointer that points to an array storage space
	   Therefore, char(*pcw)[4] = cArray; report errors
	*/
	// char(*pcw)[4] = cArray;
	
	int i = 0;
	// The memory space occupied by the output array type AINT5 and the size of the array iArray
	printf("%d, %d\n", sizeof(AINT5), sizeof(iArray));

	for(i=0; i<10; i++)
	{
		// pfArray is a pointer, and its saved value is the address of array frarray, then the content pointed by * pfArray is frarray, that is, * pfArray = frarray
		// (*pfArray)[i] = i == fArray[i] = i
		(*pfArray)[i] = i;
	}

	for(i=0; i<10; i++)
	{
		// Traverse the array elements in the array fArray to the output
		printf("%f\n", fArray[i]);
	}

	/* 
	   Since pc is a pointer, it is calculated by the pointer:
	   pc+1 = (unsigned int)pc + a·sizeof(*pc)
	   Where sizeof(*pc) is the target pointed to by pc, that is, array cArray
	   (unsigned int)pc The value saved for pc, that is, the address value of array crarray
	   pc+1 = &cArray + 9
	*/

	/* 
	   Since pcw is a pointer, it is calculated by the pointer:
	   pcw+1 = (unsigned int)pcw + a·sizeof(*pcw)
	   Where sizeof(*pcw) is the target of pcw: there are four char elements, so sizeof(*pcw) = 4
	   (unsigned int)pcw The value saved for pcw, that is, the address value of the first element of array crarray
	   pcw+1 = &cArray + 9
	*/
	printf("%p, %p\n", &cArray, pc+1, pcw+1);
	
	return 0;
}

  the meanings of all the codes in the above demo are marked in the text by annotation. At the same time, through this demo, we can clearly know the space occupied by array types, the creation of array types, the creation and use of array pointers.

  the following conclusions can be drawn from the above demo:

  • Pointer operations apply to any type of pointer
  • Array pointers are similar to ordinary pointers, but only point to arrays

Keywords: C

Added by hebisr on Thu, 16 Dec 2021 22:23:18 +0200