Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of pointers, references and STL in C++

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces C++ pointer, reference and STL sample analysis, the article is very detailed, has a certain reference value, interested friends must read!

Definition of an object: An object is a memory space that stores data and has some type of memory.

An object a that has a value and an address; when running the program, the computer allocates memory space for the object to store the value of the object, and accesses the value in the memory space through the address of the object.

Pointer, reference pointer

Type name * pointer variable name;

Each variable is stored in bytes beginning at a memory address; a pointer, also known as a pointer variable, is a four-byte (or eight-byte) variable whose contents represent a memory address; the pointer enables reading from or writing to the memory region to which it points.

int * p; //p is a pointer, variable p is of type int *T * p; //T can be any type of name, such as int, doublep Type: T**p Type: T Through the expression *p, you can read and write sizeof(T) bytes *p starting from address p Equivalent to a variable of type T stored at address p * Indirect reference operator sizeof(T*) 4 bytes (8 bytes possible on 64-bit computers) char ch2 = 'A'char *pc = &ch2; //such that pc points to variable ch2&: take address operator &x: address of variable x (i.e. pointer to x), for variable x of type T,&x indicates address of variable x (i.e. pointer to x)&x is of type T*

The Role of Pointer

With pointers, you have free access to memory space.

You can manipulate memory directly without passing variables. Through pointers, programs can access more than just the data area occupied by variables.

mutual assignment of pointers

Pointers of different types cannot be assigned to each other directly without mandatory type conversion.

pointer operation

Two pointer variables of the same type, you can compare the size (compare the size of the address space)

Two pointer variables of the same type can be subtracted (subtraction value is address space difference divided by sizeof(T))

A pointer variable adds or subtracts an integer, and the result is a pointer.

p: pointer of type T* n: variable or constant of integer type p + n: pointer of type T*, pointing to address (address p + n * sizeof(T)) n + p, p - n, *(p + n), *(p - n) are address and address pointing value eq: int a = 10;int b = 15;int * p =&a; //0x61fe04int * q = &b; //0x61fe00int * ans = p + 2; //0x61fe0cint k = *(p - 1); //15

Pointer variables can be incremented or decremented (p++, ++p, p--, --p), i.e. the address p points to n + sizeof(T) or n - sizeof(T)

Pointers can be operated with subscript operator "[]"

p is a pointer of type T* n is a variable or constant of type integer p[n] is equivalent to *(p + n)

NULL pointer

Address 0 cannot be accessed. Pointer to address 0 is null pointer

Any type of pointer can be assigned with the NULL keyword. NULL is actually the integer 0, and a pointer with NULL is a null pointer.

int *pn = NULL;char *pc = NULL;int *p2 = 0;

Pointers as function parameters (parameters are copies of arguments)

Pointers and arrays

The name of the array is a pointer constant (pointing to the starting address of the array)

T a[N];a is of type T* and can be assigned to a pointer of type T* with a. a is a constant whose value is determined at compile time and cannot be assigned to a.

T *p and T p[] are equivalent as function parameters.

void Func(int *p){ cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report