7 a pointer

7.1 Basic Concepts of Pointers

Use: access memory indirectly through Pointers

  • Memory numbers start at 0 and are usually in hexadecimal (depending on the system).
  • A pointer variable can be used to store the address of a variable

7.2 Definition and use of pointer variables

Syntax: dataType * pointVariableName = &variableName

& is a take address symbol.

You can use the dereferencing symbol * to access memory accessed by the pointer.

*pointVariableName represents the variable stored in the block of memory called variableName

7.3 Memory space occupied by Pointers

A pointer is also a data type, so how much space does this type take up?

Guess, it has to do with the system. A 64-bit system may have a 64-bit memory address, while a 32-bit system may have a 32-bit memory address.

It takes up to 4 bytes on a 32-bit system. 64-bit systems have 8 bytes. But most development environments are 32-bit. (You can also select the X64 compilation environment to switch.

7.4 Null pointer and wild pointer

Null pointer: a pointer variable points to a space in memory numbered 0

Purpose: Initialization

Note: The memory to which a null pointer points is not accessible

Syntax: dataType* pointVariableName = NULL;

Wild pointer: Pointer to an invalid memory space

This is an error, and you should try to avoid wild Pointers in your program

Syntax: int* p = (int*) 0x1100;

But actually the space 0x1100 is not applied for. No variables are declared in this space, so no memory is allocated by the system. So there’s no access. There are errors in such programs.

Const 7.5 modifies a pointer to const

Const modifies a pointer in three ways:

  1. Const modifier pointer: constant pointer const dataType* pointVariableName

    The pointer itself can be changed, but the value to which the pointer points cannot be changed through the pointer.

  2. Const modifiable variables: pointer constant dataType* const pointVariableName

    The pointer itself cannot be changed, but the value to which it points can be changed.

  3. Const modifies both a pointer and a variableconst dataType* const pointVariableName

7.6 the address

Pointers can be passed as arguments to functions. This process is called address passing, also known as addressing. After the address is passed, the actual value of the argument can be changed.

If you choose to pass an array as an argument to the function, there are two ways to pass it: int arr[]. The array can be considered a pointer, so you can pass it int* arr, and it will be passed as a pointer.