C ++ pointer (1) Data Pointer)

Source: Internet
Author: User
Pointers are favored in the C/C ++ language. Almost no C/C ++ application without pointers is found. The address used to store data and programs. This is the basic function of pointers. It is used to point to an integer, with an integer pointer (int *); to a floating point, with a floating point pointer (float *); to a structure, with a corresponding structure pointer (struct xxx *); point to any address, with a non-type pointer (void *).
Sometimes, we need some generic pointers. In C, (void *) can represent everything. But in C ++, we still have some special pointers that cannot be expressed by (void. In fact, in C ++, finding a universal pointer, especially a universal function pointer, is simply an "impossible task ".

C ++ is a static language, and type security plays an important role in C ++. In C, you can use void * to point to everything. However, in C ++, void * cannot point to everything. Even if it can, it also loses the meaning of type security. Type security can often help us find some potential bugs in the program.

Next we will discuss how to store pointers to various types of data in C ++.

C ++ pointer discussion (I) Data Pointer Mu Feng wangzhi

1. Data Pointer
There are two types of data pointers: regular data pointers and member data pointers.

1.1 Conventional Data Pointer
This should not be clear. Like the C language, definition and assignment are simple and clear. Common examples include int * and double.
For example: Int value = 123;
Int * Pn = & value;



1.2 Member Data Pointer
It has the following structure: Struct mystruct
{
Int key;
Int value;
};


There is a structure 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 the first model-regular data pointer.

Now we need a pointer that points to any data member in MyStruct, so it should be a child like this: Int MyStruct: * pMV = & MyStruct: value;
// Or
Int MyStruct: * pMK = & MyStruct: key;


This pointer is used to obtain the address of a structure member in the structure. We can use this pointer to access member data: Int value = PME-> * PMV; // obtain PME value member data.
Int key = me. * PMK; // obtain the key member data of me.


In what scenarios will the Member Data Pointer be used?
Indeed, member pointers are not commonly used. However, it is useful in some cases. Let's take a look at the following function: Int sum (mystruct * objs, int mystruct: * pm, int count)
{
Int result = 0;
For (INT I = 0; I <count; ++ I)
Result + = objs [I]. * PM;
Return result;
}


What are the functions of this function? Its function is to calculate the sum of the given member data by giving a count pointer to the MyStruct structure. It's a bit difficult, right? Look at the following program and you may understand:
MyStruct me [10] =
{
{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}, {13, 14}, {15, 16}, {17, 18}, {19, 20}
};

Int sum_value = sum (me, & MyStruct: value, 10 );
// Calculate the sum of the value members of the 10 MyStruct structures: the sum_value is 110 (2 + 4 + 6 + 8 + 20)

Int sum_key = sum (Me, & mystruct: Key, 10 );
// Calculate the sum of the key members of 10 mystruct structures: the sum_key value is 100 (1 + 3 + 5 + 7 + 19)



Maybe, you think it can be done with regular pointers, and it is easier to understand. OK, no problem: Int sum (mystruct * objs, int count)
{
Int result = 0;
For (INT I = 0; I <count; ++ I)
Result + = objs [I]. value;
Return result;
}

Do you want to do this? In doing so, you can only calculate the value. If you want to calculate the key, you need to write another function. How many members need to be computed? How many functions do you need to write.

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.