Data type and operation related knowledge of pointers in C + + summary _c language

Source: Internet
Author: User
Tags array length define null

C + + Summary of data types and pointer operations on pointers
Some pointer operations (such as p++,p+i, etc.) have been used before, and all pointer operations are listed below.

1 pointer variable plus/minus an integer
For example: P++,p--,p+i,p-i,p+-i,p-=i and so on.

C + + stipulates that a pointer variable plus/minus an integer is to add or subtract the original value of the pointer variable (an address) and the number of bytes of memory occupied by the variable it points to. If p+i represents such an address calculation: P+i*d,d is the number of bytes occupied by the variable cell that P refers to. This ensures that the p+i points to the first element below P.

2) pointer variable assignment
Assigns a variable address to a pointer variable. Such as:

 p=&a; Assign the address of variable A to P P=array//////Assign the address of the first element in the array to P
 p=&array[i];///Assign the address of the element of array I to P
 P=max;//max to a defined function. Assign the Max's entry address to P
 p1=p2//p1 and P2 are the same type of pointer variables, assigning P2 values to P1

3 The pointer variable can have a null value, that is, the pointer variable does not point to any variable, you can say this:

 P=null;


Actually null represents an integer 0, which means that P points to a cell with address 0. This allows the pointer to not point to any valid cells. In fact, the system has defined NULL first:

 #define NULL 0


The above null definition is included in the iostream header file, and null is a symbolic constant. It should be noted that the value of P is equal to NULL and P is not assigned to two different concepts.

Any pointer variable or address can be compared equal or unequal to null, such as:

 if (p==null) p=p1;

4) Two pointer variables can be subtracted
If two pointer variables point to elements of the same array, the difference between the values of the two pointer variables is the number of elements between two pointers, as shown in Figure 6.25.

If P1 points to a[1],p2 pointing to a[4], then p2-p1= (a+4)-(a+1) =4-1=3, but P1+P2 has no practical meaning.

5) Two pointer variable comparisons
If two pointers point to elements of the same array, you can compare them. The pointer variable that points to the preceding element is less than the pointer variable that points to the following element. As in Figure 6.25, P1<P2, or the value of the expression "P1<P2" is true, and the value of "P2<P1" is false. Note that it is less meaningful if P1 and P2 do not point to the same array.


6 on the assignment of pointer variable should pay attention to the type problem
The basic concepts and preliminary applications of pointers are described in the previous sections of this chapter. It should be explained that pointers are important concepts in C and C + + and are a feature of C and C + +. The advantages of using pointers are:
Improve the efficiency of the program;
When calling a function, if you change the value of certain variables in the called function, these values can be used for the calling function, that is, you can get a number of values that can be changed by the invocation of the function.
Dynamic storage allocation can be implemented.

But at the same time should see that the use of pointers is too flexible, for skilled program personnel, you can use it to write a very distinctive, quality procedures, to achieve many other high-level languages difficult to achieve the function, but also very easy to error, and this error is often difficult to find.

An array of C + + pointers and pointers to pointers
The concept of an array of pointers

If an array, whose elements are pointer type data, is called an array of pointers, that is, each element in the pointer array corresponds to a pointer variable whose value is the address. The one-dimensional pointer array is defined in the form:
Type name * array name [array length];
For example:

 int *p[4];


You can use each element of the pointer array to point to several strings, making string handling more convenient and flexible.

"Example" several strings are output in alphabetical order (from small to large).

#include <iostream>
using namespace std;
int main ()
{
 void sort (char *name[],int n);//declaring function
 void print (char *name[],int n);//declaring function
 char *name[]={ "BASIC", "FORTRAN", "C + +", "Pascal", "COBOL"}; Defines an array of pointers
 int n=5;
 Sort (name,n);
 Print (name,n);
 return 0;
}
void sort (char *name[],int N)
{
 char *temp;
 int i,j,k;
 for (i=0;i<n-1;i++)
 {
  k=i;
  for (j=i+1;j<n;j++)
   if (strcmp (name[k],name[j)) >0) k=j;
  if (k!=i)
  {
   temp=name[i];name[i]=name[k];name[k]=temp
}}} void print (char *name[],int n)
{
 int i;
 for (i=0;i<n;i++)
  cout<<name[i]<<endl;
}

The results of the operation are:

BASIC
COBOL
C + +
FORTRAN
Pascal


The function of the print function is to output individual strings. NAME[0]~NAME[4] is the first address of each string. The print function can also be rewritten in the following form:

void print (char *name[],int n)
{
 int i=0
 char *p;
 P=NAME[0];
 while (I<n)
 {
  p=* (name+i++);
  cout<<p<<endl;
 }
}

where "* (name+i++)" means first the value of the * (Name+i), that is, name[i] (it is an address). Assign it to P, then I plus 1. Finally, the string that starts with the P address is printed.
Pointer to pointer

On the basis of mastering the concept of an array of pointers, the following describes pointers to pointer data, referred to as pointers to pointers. As you can see from figure 6.22, name is an array of pointers, each of which is a pointer data (whose value is an address), pointing to a different string. The array name name represents the address of the first element of the pointer array. Name+i is the address of name[i]. Because the value of name[i] is an address (that is, a pointer), Name+i is a pointer to pointer data. You can also set a pointer variable p, which points to an element of the pointer array (see diagram). P is the pointer variable that points to the pointer type of data.

How do you define a pointer variable that points to pointer data? As follows:

 char * (*P);


As you can see from Appendix B, the Binding of the * operator is from right to left, so "char * (*P);" Can be written as:

 Char **p;

"Example" is a pointer variable that points to character data.

#include <iostream>
using namespace std;
int main ()
{
 char **p;//define pointer variable p char *name[]={to character pointer data
 "BASIC", "FORTRAN", "C + +", "Pascal", "COBOL"};
 p=name+2; See the
 string
 cout<<**p<<endl;//Output name[2 of P in figure 6.23 pointing to cout<<*p<<endl;//output NAME[2] The first character in the string to point to

The results of the operation are:

C + +


The elements of an array of pointers can also point not to strings, but to integral or single-precision data.

The way to "indirectly access" a variable has been mentioned at the beginning of this chapter. Accessing another variable with a pointer variable is "indirect access." If you hold the address of a target variable in a pointer variable, this is a "single-level inter-address", as shown in Figure 6.24 (a). Pointers to pointers use the "two-level address" method. See the figure below. Theoretically, the inter-address method can be extended to more levels, as shown in the following figure. In practice, however, there are rarely more than two-tier addresses in the program.



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.