Language basics: C language Pointers.

Structure struct

C tools that can be used to describe and define new types.

// Struct: point type
struct point {
  // Describe the components of this type
  int x, y;
}
Copy the code

In a program, there are basic data types, and then new data types are defined based on those basic data types.

We can also use our own definition of the new type to define another updated type.

Base data types (or function modules) are the data types (or function modules) built into the programming language.

The size of a structure variable

Both Data1 and Data2 structures are composed of two character fields and an integer field. However, the storage space occupied by the two is not the same.

  1. Built-in types take up several bytes, in bytes; Structure variables are stored in units of storage.

    For a structure type, the size of the storage cell is equal to the number of bytes used by the base type that occupies the most space.

    The principle of minimum deposit.

  2. The fields of a structure are stored in memory in the order in which the structure was defined. And when there is not enough space in one storage unit, start at the head of the next storage unit.

When designing a structure, you not only need to design the data fields contained in the new structure type, but also need to pay attention to the order between the fields.

Structures are useful tools for creating new types. The storage space occupied by the structure type depends on the composition of the internal fields and the order of each field.

Pointer variables are also variables

A value of any type is stored in a variable of any type.

Pointer variable, a variable that stores addresses.

The * operator precedes the variable name and is called the “value” operator. For example, *p indicates the content of the storage area pointed to by the value p.

// the pointer variable p
// Find the first address of a storage area based on the contents of the variable p, and then determine how many (4) bytes of the contents of the storage area based on the type of p (int).
int *p;
Copy the code

The type of a pointer variable, in addition to determining the value, determine the size of the overwrite storage area, can also be added or subtracted.

Values of the address type support addition and subtraction.

The pointer variable type determines how much address length to add or subtract when adding or subtracting.

The size of a pointer variable

Regardless of the type of pointer, the value stored is the address of a byte. In a system, the binary data is the same length, regardless of the byte address. So, no matter what type of pointer, the underlying representation length of the value that needs to be stored is the same, that is, the amount of storage used is the same.

Struct variables refer to fields, which refer directly to (.) ; If a pointer wants to reference a field, it is an indirect reference (->).

Pointer to the summary

  1. The type of the pointer, which determines the number of bytes taken from the pointer;
  2. The type of pointer determines the number of bytes to span during pointer addition and subtraction.
  3. Pointers are of the same size regardless of type (address information is uniform).