The use of pointers can directly manipulate the memory address, improve efficiency, the disadvantage is detours more difficult to understand, this tutorial for you to introduce the pointers in C + + language.
1, start Geany
1 point Menu "application-programming-geany" Start Geany, create a new C + + source program;
2 Point Menu "file-Save as" command, to "PTR" as the filename, save the file to its own folder;
2. Pointer variable
1 Use the pointer before you also define, with the * number to define, assignment with the address of other variables, can also be initialized to null;
Enter the following code:
int a = 25;
int *p = NULL;
p = &a;
2 The first sentence defines an integer variable A, each variable has a memory address, with the &a command to get the address of the variable,
The second sentence defines a pointer variable, the type is integral, the asterisk * is the definition symbol, and Null indicates that the assignment is null.
The third sentence is to assign a value to the pointer variable p, the value is the address of the variable A, formatted like 0x123456;
3) Then we will show the values of each variable, corresponding to their respective types;
cout << "a =" << A;
cout << "&a=" << &a;
cout << "p =" << p;
4 A is an integer variable, which is filled with integers, &a is the address operator, and gets the address,
P is a pointer variable, which holds the address, p gives the address of a, so two addresses are the same;
5 Next we look at using the pointer to manipulate its corresponding variable, continue to enter the following code;
cout << "*p=" << *p << Endl;
6 The *p here is not a definition pointer, because the asterisk * has no type before, and P has been defined and cannot be defined repeatedly,
The *p here is an indirect reference, pointing to A, because P holds the address of a, so the *p value is 25;
7 using *P can operate on variable A, for example, the following code assigns a value to A;
*p = 16;
cout << "a =" << A;
#include <iostream>
using namespace Std;
int main (int argc, char** argv)
{
int a = 25;
int *p = NULL; Defines the pointer variable and initializes the
p = &a; assigning to pointer variables
cout << "a =" << a << Endl;
cout << "&a=" << &a << Endl;
cout << "p =" << p << Endl;
cout << Endl;
cout << "*p=" << *p << Endl;
cout << Endl;
*p = 16; Indirect references, not defining pointers
cout << "a =" << A;
return 0;
}