A study of C + + pointers (i) Data pointers

Source: Internet
Author: User

pointers, which have always been very favored in the C + + language; A C + + application that does not use pointers is almost impossible to find. The address used to store data and programs, which is the basic function of pointers. Used to point to integers, with integer pointers (int*), floating point pointers (float*) pointing to floating-point numbers, pointing to structures, using corresponding structure pointers (struct XXX *), pointing to arbitrary addresses, with no type pointer (void*).

Sometimes, we need some generic pointers. In C, (void*) can represent everything, but in C + +, we have some special pointers that cannot be represented by (void*). In fact, in C + +, trying to find a generic pointer, especially a generic function pointer, is simply an "impossible task."

C + + is a static type of language, and type safety is important in C + +. In C, you can use void* to point to everything, but in C + +, void* does not point to everything, and even if it does, it loses the meaning of type safety. Type safety can often help us find some potential bugs in the program.

Let's explore how to store pointers to various types of data in C + +.

1. Data pointers

Data pointers are divided into two types: general data pointers and member data pointers

1.1 General Data pointers

This does not have to be clear, as with C language, definition, assignment is very simple and clear. Common are: int*, double* and so on.

Such as:

int value = 123;

int * pn = &value;

1.2 Member data pointers

A structure that resembles the following:

struct MyStruct
{
int key;
int value;
};

Now there is a struct object:

MyStruct me;

mystruct* pMe = &me;

We need the address of the value member, we can:

int * Pvalue = &me.value;

Or

int * Pvalue = &pMe->value;

Of course, this pointer is still part of the first areas-general data pointer.

Well, now we need a pointer that points to any data member in the MyStruct, so it's supposed to be a child:

int mystruct::* PMV = &MyStruct::value;

Or

int mystruct::* PMK = &MyStruct::key;

The purpose of this pointer is to obtain the address of the struct member within the structure. We can use this pointer to access the member data:

int value = pme->*pmv;//Get PMe value member data.

int key = ME.*PMK//Get key member data for me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.