Pointer-based data type and pointer operation summary _c language

Source: Internet
Author: User
Tags define null

1. Summary of data types of pointers

About the data type of the pointer

definition meaning
int i; Defining Shaping variables
int *p; Defines a pointer variable p that only wants integer data
int a[n]; Defines an array of integers a, which has n elements
int *p[n]; Defines the pointer array p, which consists of n pointer elements that point to the shaping data type
int (*p) [n]; A pointer variable that defines an array that points to n elements
int f (); F is a function that takes back the function value of the reshaping
int *p (); P is a function that brings back a pointer that points to the shaping data.
Int (*p) (); P is a pointer to a function that returns an orthopedic data
int * *P; P is a pointer variable to a pointer that points to a pointer variable that is shaping the data

2. Summary of the operation of pointers
(1) Assignment of pointer variable
Copy Code code as follows:

int A;
int *p
p=&a;

Assign the address of variable A to P
Copy Code code as follows:

int a[3]={1,2,3};
int *p;
P=a;

Assigns the address of the first element of the array to the pointer p
Copy Code code as follows:

int a[3]={1,2,3};
int *p;
p=&a[2];

Assigns the address of an element in an array to the pointer p
Copy Code code as follows:

int main () {
int f (int z);
Int (*p) (int z);
P=f;
P (5);
}
int f (int z) {
cout<<z<<endl;
}

F is an already defined function that assigns the entrance address of F to P
Copy Code code as follows:

int a=3;
int *p1=&a;
int *p2=p1;

P1 and P2 are pointers of the same type, assigning P1 values to P2

(2) Null value of pointer variable
A pointer variable can have a null value, that is, the pointer variable does not point to any variable, which can be represented as:

Copy Code code as follows:

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 already defined null:
Copy Code code as follows:

#define NULL 0

In C + +, the above null definition is included in the iostream header file, and null makes a symbolic constant.
To run in the C-free editor:
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
cout<<null;
cout<<endl;
}




It should be noted that the value of P equals null and P is not assigned to two concepts. The former is worthwhile (a value of 0) and does not point to any variable, although the latter is not assigned to p but does not equal p without value, but its value is an unpredictable value, that is, p may point to an unspecified cell. This is a very dangerous situation. Therefore, it must be assigned before drinking the variable.

Any pointer variable or address can be compared to or unequal to null:

Copy Code code as follows:

if (p==null) p=p1;

The above statement can also be written as:
Copy Code code as follows:

if (!p) p=p1;

should also note
the difference between int *p=null; and int *p;*p=null:
int *p=null is a pointer that defines an integer variable, and initializes the pointer to null;
the int *p; Defines a pointer to an integer variable because the pointer is not initialized, so it may point to any value, so it may point to an illegal value, such as a variable in system memory.

Then *p =null is that the value of the variable that P points to is 0, because the value that P points to is indeterminate, so the operation is very dangerous.

(3) The problem that the assignment of pointer variable should pay attention to
We know that different pointer variables with the same base type can be assigned to each other.
Variables of different base types cannot be assigned.
To run the code:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
<span style= "White-space:pre" > </span>int *p1,i=5;
<span style= "White-space:pre" > </span>double *p2, j=2.5;
<span style= "White-space:pre" > </span>p1=&i;
<span style= "White-space:pre" > </span>p2=&j;
P1=P2;
cout<<*p1<<endl;
return 0;
}

Editor Tip:



You can implement the above assignment by forcing type conversions:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int *p1,i=5;
Double *P2, j=2.5;
p1=&i;
cout<<*p1<<endl;
p2=&j;
cout<<*p2<<endl;
p1= (int *) P2;
cout<<*p1<<endl;
return 0;
}

Although the above action no longer complains, but the pointer to the mandatory type of data truncation occurred, so still do not get the desired results:



About coercion type conversions of pointers

(4) pointer variable plus/minus an integer

For example:

Copy Code code as follows:

p++;
p--;
P+i;
P-1;
P+=i;
P-=i;

C + + stipulates that a pointer variable plus/minus an integer is to add or subtract the original value of the pointer variable (the original point to the address) and the number of bytes of memory occupied by the variable it points to.

such as P+i, which represents the address calculation: P+i*d,d is the number of bytes occupied by the variable cell that P points to. This ensures that the p+i points to the first element below P.

(5) subtracting two pointer variables
If two pointers point to elements in the same array, the difference between the two pointer variables is the number of elements between the two pointers.

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
int *p1=&a[3];
int *p2=&a[5];
cout<< (P2-P1) <<endl;
cout<< (P1-P2) <<endl;
return 0;
}

Run Result:



(6) Two pointer variable comparisons
If two pointers point to the elements of the same array, you can compare the size. The pointer variable that points to the preceding element is less than the pointer variable that points to the following element.
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
int *p1=&a[3];
int *p2=&a[5];
if (P1<P2) {
cout<< "P1<P2" <<endl;
}else{
cout<< "P1>=P2" <<endl;
}
return 0;
}

Result output:


You can also use this property to output all of the elements in an array:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
int *p=a;
while (P<A+10) {
cout<<*p<<endl;
p++;
}
return 0;
}

Output results:

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.