C + + pointers

Source: Internet
Author: User

Pointers are one of the important concepts in C/s + + programming and one of the most confusing and error-prone problems. The use of pointer programming can represent a variety of data structures, through the use of pointers can be used to share variables or data structures between the main function and the function, easy to achieve two-way data communication, the pointer can be flexible operation of memory, reasonable operation of memory can make the program more efficient.

1. The concept of pointers
The pointer is essentially a variable, the normal variable contains the actual data, and the pointer variable contains an in-memory address, which points to a variable or function, the pointer is the address. The pointer is an indicator that tells the program which area of the memory is in which to find the data.

2. The contents of the pointer
The contents of the pointer contain 4 parts: the type of the pointer, the type that the pointer points to, the value of the pointer, and the memory area that the pointer itself occupies. When learning pointers, the type of pointers and the type of pointers pointed to are extremely confusing, and figuring out these concepts helps us to use pointers correctly.

3. Type of pointer and type to which the pointer is pointing
Syntactically, a pointer is a type that removes the rest of the pointer's name from the cursor declaration statement.
The pointer points to a chunk of memory, and the type that the pointer points to depends on what type of compile-time is present within the block, such as the type that a int* type pointer points to is int.
Let me take a few examples to illustrate these two concepts.

    1. int p;//This is just a normal variable.
    2. Int* p;//int* also represents a data type: int pointer type. So the type of P is: int* type, p points to type int

Here, a little pause. Teach you how to look at the pointer type and the type that the pointer points to. (My own understanding)  
for the int*p example above, it can be written as int* p or as an int *p. The first kind of understanding is biased to the address, that is, p is an address variable, p is a hexadecimal address, and the second is biased toward a value, *p is an integer variable that can represent an integral type value.  
Both of these are correct, but understand the differences, but I think it is perfectly possible to combine the two concepts of pointer type and the type that the pointer points to.  
Think about the steps we use the pointer: Declare the pointer, assign a value to the pointer, and then use the value pointed to by the pointer.  
We all know that a pointer is a composite data type that must be combined with a basic type to form a pointer type.  
then int* is a composite data type-the integer pointer type.  
This is a good way to explain the first notation, and at the time of Declaration, int* P directly declares that the P variable is an integer pointer type, which is the initial step.  
The second step is to assign a value to the pointer, p is the pointer type, it holds the address, and here is the assumption that I am assigning a value to it: P = & Ordinary variable, (for example, int a = 5;p=&a;).  
The third step uses pointers in the C + + primer to explain in detail that the * is the dereference operator (which I understand is the address resolution operator), and if p is the address, then *p is the actual value (such as the corresponding *p = 5). For starters, when it comes to understanding what it means, it's perfectly possible to cross this step, which says that both int* p and int *p when declaring pointers, I prefer the first understanding when I use it, and I prefer the second understanding when used: after all, we use values, and *p is the value.  
My conclusion: for the understanding of int* p and int *p (which is also an understanding of the pointer type and the type that the pointer points to), a pointer contains two parts, an address and a value, and the pointer declares an address variable (the pointer is the address), using the value pointed to by the pointer. Or the pointer contains two types: the pointer type and the type to which the pointer points, declaring the pointer type when declared, using the type pointed to by the pointer.

    1. int p[3];//p First and [], stating that P is an array, and then a combination of int, so p is an int type array
    2. int* p[3];//Priority [] is higher than *, p is an array, plus int*, it can be called an array of pointers, the value of each element of an array is a pointer (address)
    3. int (*p) [3];//*p can be considered as a normal variable and goes back to the third case (int p[3]), but here p is a pointer variable that points to an array of 3 integer values. It can be called an array pointer, and the value of each element in the arrays is a normal integer value.
    4. int** p;//int* represents a pointer type, * is a pointer type, so p is a pointer to an int pointer, and p is a int* type (int type pointer)
    5. int p (int);//This is obviously a function with a return type of int and a parameter of type int
    6. Int (*p) (int);//p is a function pointer to a function that returns a value of int with an int parameter. This declaration consists of two parts: the function variable + the function address variable (the function is also considered a variable)
    7. int* (*p (int)) [3];//This is a bit complicated, it's still a function pointer. from *p (int), it is a function pointer with an int parameter, and then look at [], indicating that the function returns an array, and then returns a type of int*. So P is a pointer to a function that returns a pointer array of type int* and with an int argument

In the normal use, we just need to understand the previous few can be, too complex pointers are basically not used, readability is not good.

Here are some simple examples of pointers.

Demo1 & and * operators

    1. #include
    2. int main ()
    3. {
    4. using namespace Std;
    5. int updates = 6;
    6. int *p_updates;
    7. P_updates = &updates;
    8. cout<< "values:updates =" <<updates;
    9. cout<< ", *p_updates =" <<*p_updates<<endl;
    10. cout<< "Address: &updates =" <<&updates;
    11. cout<< ", p_updates =" <<p_updates<<endl;
    12. *p_updates = *p_updates + 1;
    13. cout<< "Now updates =" <<updates<<endl;
    14. }

Demo2

    1. #include
    2. int main ()
    3. {
    4. using namespace Std;
    5. int higgens = 5;
    6. int* pt = &higgens;//is an assignment to PT, not a *pt. Equivalent to int* pt; PT = &higgens;
    7. cout<< "Value of Higgens =" <
    8. cout<< "addnress of Higgens =" <<&higgens<<endl;
    9. cout<< "Value of *pt =" <<*pt<<endl;
    10. cout<< "Value of pt =" <<pt<<endl;
    11. return 0;
    12. }

Demo3 New and delete operators

    1. #include
    2. int main ()
    3. {
    4. using namespace Std;
    5. int* pt = new int;
    6. *PT = 1001;
    7. cout<< "int value =" <<*pt<< ", and location =" <<pt<<endl;
    8. double* PD = new Double;
    9. *PD = 5.234;
    10. cout<< "Double value =" <<*pd<< ", and location =" <<pd<<endl;
    11. Delete pt;
    12. Delete PD;
    13. return 0;
    14. }

Demo4 Dynamic Array

  1. #include
  2. int main ()
  3. {
  4. using namespace Std;
  5. double* p3 = new Double[3];
  6. P3[0] = 0.2;
  7. P3[1] = 0.5;
  8. P3[2] = 0.8;
  9. Cout<<sizeof (p3) <<endl;
  10. cout<< "P3[1" is "<<p3[1]<<". \ n ";
  11. P3 = p3 + 1;//adds one bit of address, where one unit standard is the number of bytes of memory that are based on an element of the array.
  12. cout<< "Now p3[0" are "<<p3[0]<<" and P3[1 "is" <<p3[1]<< ". \ n";
  13. Cout<<sizeof (p3) <<endl;
  14. P3 = p3-1;
  15. Delete[] P3;
  16. return 0;
  17. }

Demo 5 arrays and pointers

  1. #include
  2. int main ()
  3. {
  4. using namespace Std;
  5. Double Wages[3] = {10000.0,20000.0,30000.0};
  6. Short Stacks[3] = {3,2,1};
  7. Two ways to get the address of an array--the array name or the first element of a set of elements for addressing operations
  8. double* pw = wages;
  9. short* PS = &stacks[0];
  10. cout<< "PW =" <<pw<< ", *PW =" <<*pw<<endl;//prints the value and address of the first element of the array
  11. PW = pw + 1;
  12. cout<< "Add 1 to the PW pointer:\n";
  13. cout<< "PW =" <<pw<< ", *PW =" <<*pw<< "\ n";
  14. cout<< "PS =" <<ps<< ", *ps =" <<*ps<<endl;//prints the value and address of the first element of the array
  15. PS = PS + 1;
  16. cout<< "Add 1 to the PS pointer:\n";
  17. cout<< "PW =" <<ps<< ", *ps =" <<*ps<< "\ n";
  18. STACKS[1] equivalent to * (stacks + 1)
  19. cout<< "Access elements with array notation\n";
  20. cout<< "stacks[0] =" <<stacks[0]<< ", stack[1] =" <<stacks[1]<<endl;
  21. cout<< "Access elements with pointer notation\n";
  22. cout<< "*stacks =" <<*stacks<< ", * (stacks + 1) =" <<* (stacks + 1) <<endl;
  23. Cout<<sizeof (wages) << "= Size of Wages array\n"; 24 bytes
  24. Cout<<sizeof (PW) << "= size of PW pointer\n"; 4 bytes
  25. return 0;
  26. }

Demo6 String and Address

    1. #include
    2. int main ()
    3. {
    4. using namespace Std;
    5. Char flower[10] = "Rose";
    6. The string sends the address to the Cout object.
    7. In cout and most C + + expressions, char array names, pointers to char, and string constants enclosed in quotation marks are interpreted as addresses of the first character of a string
    8. cout<<flower<< "s is red.\n";
    9. return 0;
    10. }

Demo7 String and Address

  1. #include
  2. #include
  3. int main ()
  4. {
  5. using namespace Std;
  6. Char animal[20] = "Bear";
  7. Const char* Bird = "wren";//bird the address where the string is stored
  8. char* PS;
  9. cout<<animal<< "and";
  10. cout<<bird<< "\ n";
  11. cout<< "Enter A kind of animal:";
  12. cin>>animal;
  13. cin>>ps;//does not allocate memory space for PS, it is wrong to do this
  14. PS = animal;//ps and animal point to the same string and memory unit
  15. cout<<ps<< "s!\n";
  16. cout<< "before using strcpy (): \ n";
  17. cout<<animal<< "at" << (int*) animal<<endl;//calculates the address of the animal, and if it is a char* type, prints out the string
  18. cout<<ps<< "at" << (int*) ps<<endl;
  19. PS = new Char[strlen (animal) + 1];//allocation of additional memory space
  20. strcpy (ps,animal);//array copy, if the animal is assigned to PS, this will change the address of PS, we will not be able to manipulate the newly allocated memory
  21. cout<< "after using strcpy (): \ n";
  22. Although animal and PS point to the same string, their memory addresses are different
  23. cout<<animal<< "at" << (int*) animal<<endl;
  24. cout<<ps<< "at" << (int*) ps<<endl;
  25. Delete[] PS;
  26. return 0;
  27. }

DEMO8 dynamic Structure--pointer operator

  1. #include
  2. struct INFLATABLE
  3. {
  4. Char name[20];
  5. float volume;
  6. Double Price;
  7. };
  8. int main ()
  9. {
  10. using namespace Std;
  11. inflatable* PS = new inflatable;//allocates memory at run time
  12. cout<< "Enter Name of inflatable item:";
  13. Cin.get (Ps->name, 20);
  14. cout<< "Enter volume in Cubic feet:";
  15. Cin>> (*PS). Volume;
  16. cout<< "Enter price:$";
  17. cin>>ps->price;
  18. cout<< "Name:" << (*PS) .name<<endl;
  19. cout<< "Volume:" <<ps->volume<< "cubic feet\n";
  20. cout<< "price:$" <<ps->price<<endl;
  21. Delete PS;
  22. return 0;
  23. }
4. The value of the pointer (or the memory area pointed to by the pointer)

The value of the pointer, or the memory area or address that the pointer points to, is the value stored by the pointer itself, which is treated as an address by the compiler and not as a generic value. In a 32-bit program, the value of all types of pointers is a 32-bit integer because the memory address in the 32-bit program is all 32 bits long. The memory area that the pointer points to is the memory address from which the value of the pointer is represented, and the length of the memory area of sizeof (the type the pointer is pointing to). Later, we say that the value of a pointer is XX, which is equivalent to saying that the pointer to an XX-led address of a piece of memory area; we say that a pointer points to a region of memory, which is equivalent to saying that the value of the pointer is the first address of the memory area.

The memory area that the pointer points to and the type that the pointer points to are two completely different concepts. In the example above, the pointer points to a type that already exists, but because the pointer is not initialized, the memory area it points to is nonexistent, or meaningless.

Later, every time you encounter a pointer, you should ask: What is the type of this pointer? What is the type of pointer? Where does the pointer point?

5. The memory area occupied by the pointer itself

The memory area that the pointer itself occupies is the size of the pointer itself, which you will know by using the function sizeof (the type of the pointer). In a 32-bit platform, the pointer itself occupies a length of 4 bytes.

The concept of memory occupied by the pointer itself is useful when judging whether a pointer expression is an lvalue.

C + + pointers

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.