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

How to use pointers in C++

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use pointers in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Why should pointers have types?

It's for pointer operations and values.

When using a pointer to take a value, you need to know how to take a value, such as how many bytes to take the value, which needs to be determined to get the correct value, and to know how many bytes to take, you have to know what the type of pointer is.

We know that increasing or decreasing the pointer operation by 1 means offsetting the number of bytes of the type represented by the pointer. For example, an int byte pointer increases by 1, indicating an offset of 4 bytes (usually int is 4 bytes), so it is also necessary to know the type of pointer.

Pointers and arrays

Originally, literally speaking, pointers and arrays can not be hit by eight poles, they are supposed to be that the well water does not interfere with the river water, how can they be involved? We often hear about array pointers and pointer arrays. What do they mean? Are they a pointer or an array? The following will answer for you one by one.

Pointer array, first of all, it is an array, each element in the array is a pointer, for example, int * p [4] is a pointer array, because the precedence operator * of operator [] is high, so p takes precedence with [] to form an array, and then * and type int are combined into the type of array elements. For example, the following program is an example of a pointer array:

Main.c#include int main () {char * str [3] = {"I am Array 1", "I am Array 2", "I am Array 3"}; printf ("% s\ n% s\ n% s\ n", str [0], str [1], str [2]); return 0;}

Array pointer, first of all, it is a pointer, the pointer to the object is an array, such as this pointer is p, then dereferencing * p to get the content is an array, such as int (* p) [4], the idea to put parentheses, the array pointer is usually used as a two-dimensional array.

Secondary pointer

The so-called secondary pointer is actually a pointer to a pointer. For example, int * * p is a secondary pointer, and the object stored in it is a pointer. The address of the pointer stored in memory is obtained by dereferencing once. You need to dereferencing the internal pointer again to get the value of the real content.

It's a little circuitous to understand, so what's the use of this mouthful second-level pointer? Secondary pointers may not be used much in C++, but they are often used in C #. It is usually used as a parameter of a function to initialize a pointer inside the function. For example, the classic audio and video processing tool FFmpeg uses a lot of secondary pointers. The following example shows how to assign a value to a pointer through a secondary pointer:

Main.cppvoid initP (int * * p) {* p = new int (10);} int main () {int * p = nullptr; / / an empty pointer initP (& p); / / initialize the pointer p std::cout with a secondary pointer

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