C Language_Pointer Elementary (1)

1. What is a pointer:

Two key points to understand pointers:

1. A pointer is the number of a smallest unit in memory, that is, the address.
2. The pointers mentioned in normal spoken language usually refer to pointer variables, which are variables used to store memory addresses.

Summarize:

- A pointer is an address. The pointer in spoken language usually refers to a pointer variable - a pointer variable is used to store the address variable. (The values ​​stored in pointers are treated as addresses) - Pointer variables are used to store address variables. Addresses uniquely identify a memory unit - How big is a small unit? How to address? For a 32-bit machine, assuming there are 32 address lines, if no address line generates high level (high voltage) and low level (low voltage) during addressing, it will be (1 or 0), and there will be 2 . 32 addresses; each address identifies a byte... 4G space for addressing.

2. Pointers and pointer types

- Pointer variables of different types are 4 bytes on 32-bit platforms - Pointer variables of different types are 8 bytes on 64-bit platforms

3. The meaning of pointer types

  • 1. The pointer type can determine how many bytes are accessed when the pointer is dereferenced (the permissions of the pointer)

Pointer dereference of int * and pointer dereference of char *

The pointer type determines the step size of the pointer +/-1 operation;

The integer pointer +1 skips 4 bytes, and
the character pointer +1 skips 1 byte.

The step size to skip when +/-n;

pointer of type * p

What is skipped is n * sizeof(type) bytes

4. Wild pointer

  • Concept: A wild pointer means that the position pointed by the pointer is unknown (random, incorrect, and without clear restrictions)
  • 4.1 Causes of wild pointers
  • 1.The pointer is not initialized
  • 2. Pointer out-of-bounds access
  • 3. Release the space pointed by the pointer

  • 4.2 How to avoid wild pointers

  • 1. Pointer initialization

(1). If you know clearly whose address the pointer should be initialized to, just initialize it directly.
(2). I don’t know what value the pointer is initialized to. It is temporarily initialized to NULL.

ptr is a null pointer and does not point to any valid space. This pointer cannot be used directly.

    1. Be careful the pointer goes out of bounds
    1. The space pointed to by the pointer is released and set to NULL immediately.
    1. Avoid returning the address of local variables
    1. Check validity of pointers before using

5. Pointer arithmetic

  • 5.1 Pointer ± integer
    Example:
  • 5.2 Pointer-

    Pointer The prerequisite for pointer-pointer operation is that pointer and pointer point to the same space.
  • 5.3 Relational operation of pointers (size comparison)

    The address has a size.
    The relational operation of pointers is to compare the size of pointers.

Supongo que te gusta

Origin blog.csdn.net/Ghr_C99/article/details/130797741
Recomendado
Clasificación