C-language kill device---pointers to detailed __c language

Source: Internet
Author: User
Tags constant modifier

One, the pointer

1, the concept of the pointer: to save the address of the "variable" is called a pointer, can be understood as a pointer is an alias of the address.


Example: Defining an shaping pointer


2, "The contents of the pointer", "what the pointer points to", "the address of the pointer variable"

(1), the contents of the pointer:

The pointer variable p contains the address of a, which is 0x0018ff44.

(2), what the pointer points to:

The value of the space corresponding to the address (0X18FF44) in which the pointer variable p resides, that is, 10, which we can access through *p (dereference). That is: *p as the right value, *p==10, when *p as the left value is represented by a this space.

(3), the address of the pointer:

Pointer P itself has an address, which is assigned by the compiler we do not know, note: Do not speak of the pointer variable p itself address and P saved address (0x0018ff44) confused.


3, "uninitialized pointers" and "null pointers"

Example: Defines two pointer variables p1 and p2:

int *p1; Uninitialized pointers

int *p2=null; Null pointer

*p1=10;

*p2=10;

This assignment is obviously wrong, why ...

Imagine that P1,P2 is a pointer variable, so P1,P2 should be stored in an address. And for P1 we did not put the address inside, when the P1 was randomly pointed. If you refer to *P1 at this time, access to a block of random address, and then modify the value of the random address, assuming that the value of the random space is very important, then you can not find back, so usually define a pointer to initialize him to null.

For P2: Although we initialize it to NULL, it points to a space site ... It is impossible to put something in an open space site.


4. Pointer constants:

Example: * (int *) 200=10;

I believe many people have doubts about this expression, in fact, it is very well understood that 200 is a constant, we will force it into an int * (that is, the constant 200 into a plastic address), and then to the address of the * reference, access to a space, can be assigned to this space.


5. Constant pointer

Example: int *p=&100;

Let the pointer point to a constant, which is generally not used.


Second, advanced pointers


1, two-level pointer concept:

What is a pointer ... The variable that holds the address is called a pointer, so the level two pointer is the pointer variable that holds the first-level pointer address.

Note: Like the first-level pointer, the two-level pointer variable P2 contains the address of the first-level pointer variable P1, and the first pointer variable P1 contains the address of a. To access the contents of a piece of address, you can use the indirect accessor "*", so:

*P2==&P1, *P2 is to access the P2 of this space in the address of the corresponding content.

**p2==10, because *P2 gets the result is P1 address, in P1 address to dereference *p1 to access to 10.


Example 1: Analyze the following conditions can be as modifiable left value (the modifiable left value must represent a piece of space)

int a=10;

int *cp=&a;


(1) *cp=20//Can be used as the left value, when the CP points to a, the *CP is placed on the left side of the equal sign representing a space, when *CP to the right of the equal sign represents a space value.


(2) &a=20//error, &a can not be used as the left value, because he can not represent a specific space, &a the result is the address of a, but does not represent a space, to use this space, must be * reference, *&a=20 correct. The &a can be used as the right value, representing the address of a.


(3) *cp+1//Can not be used as the left value, because * priority is higher than +, so *CP first combined, plus 1 is equivalent to 10+1, does not represent a piece of space.


(4) * (cp+1)//can be used as the left value, cp+1 first, point to the next space of a, and then the space for the * reference, placed on the left side of the equal sign represents the space.


(5) ++CP//Can not be used as the left value, because ++CP just add the contents of the CP to one, and no * reference


(6) cp++//Can not be used as the left value


(7) *cp++//Can be used as the left value, first to the CP inside address for * reference. Let Cp=cp+1 (that is, let CP point to the next space of a, because + + priority is higher than *)


(8) ++*CP//Can not be used as the left value, *CP represents the content of CP, and then let it add one, equivalent to 10+1

Note: Both ++CP and--CP in C can not do the left value, ++CP in C + + as the left value.


Example 2:const modifies a first-level pointer, modifies a two-level pointer (a const-modified variable, or a variable, but only readable)

int const A=10;

int b=30;

1, a=20; Error, const modifies A, so you cannot change the value of a


2, int const *p; The const modifier is *p, so it cannot be changed *p

p=&a; That's right

*p=20; Error cannot change the value of a by *p


3, const int *p; The const modifier is *p.

p=&a; That's right

*p=20; Error


4, int *const p=&a; The const modifier is p.

p=&b; Error cannot change p

*p=20; That's right


5, int const * Const p; The const modifier *p, also modifies p, so *p,p can not change

p=&b; Error

*p=20; Error

Note: The principle of the const modifier variable is who is nearly modified from WHO. The const int *P is exactly the same as the int const *p.


2, the relationship between pointers and arrays, pointer arrays, array pointers, operation of pointers


2.1, the relationship between the pointer and array:

Many people are confused about the relationship between pointers and arrays, and strictly speaking, the pointer is a pointer and the array is an array. It's just that both of them can access the element through the "*" Reference and the subscript way.


Example 1:

int a[5]={1,2,3,4,5};

int *p=a;

A[5] is 20 bytes in size, and p is only 4 bytes in size, followed by P itself having its own address, except that it contains the address of the first element of the array.

There are two ways to access 3: a[2] or * (a+2).

where * (a+2) is the form of * access, because a represents the address of the first element, plus 2 means to offset the 2 size backward, find 3 of the address, in the pass * to get 3.

A[2] In the compiler is first parsed into * (a+2) access.


Example 2:

So you have to keep the consistency of definitions and declarations, pointers are pointers, arrays are arrays.


3, pointer array, array pointer

NOTE: [] has precedence over *, the pointer array is an array, except that the elements inside are all pointers. The array pointer is a pointer to the array pointer, and the offset unit is the entire array.


Example 1:

int a[6]={1,2,3,4,5,6};

int (*P2) [6];

P2=a;

This is wrong because the type of pointer p2 is int [6], so it should be p2=&a;

int (*P2) [3];

In this case the P2 type is int [3], so p2= (int (*) [3]) &a; The type of the array pointer to be cast.

Note that the type of the array pointer "points" is what remains after the pointer variable is removed.

The type of the array pointer is what remains after the pointer is removed.


Example 2:int (*P3) [5];

The type of P3 is int (*) [5];

The type that the P3 points to is int [5];


4, the operation of the pointer:

1, the result of the pointer subtraction is the number of elements between the two pointers, not the number of bytes.

2, the operation of the pointer

Example 1:

struct test

{

int name;

Char *pcname;

Short data;

Char c[2];

}*p;

Suppose the value of P is 0x100000, the size of the structure is 12;

The

1, p+0x1=0x10000c//p pointing to the structure of the p+1 is p+sizeof (test) *1


2, (unsigned int) p+0x1=0x100001//p has been converted to an unsigned number, plus one is equivalent to two numbers added


3, (int *) p+0x1=0x100004//p is converted to int *, so p+1 represents p+sizeof (int *) *1


Example 2: Assuming that the current machine small-end storage

int a[4] = {1, 2, 3, 4};

int *p = (int *) (a + 1);

int *P1 = (int *) (&a + 1);

int *P2 = (int *) ((int) &a + 1);

printf ("*p=%d,*p1=%d,*p2=%d\n", *p, P1[-1],*P2);


Output Result: *p=2,*p1=4,*p2=2000000

Parsing: p = (int *) (a + 1); A represents the first element address, plus 1 points to the second element, so it's 2.

P1 = (int *) (&a + 1); &a represents the address of an array, &a+1 is equivalent to &a+sizeof (a), P1 is pointing to the address at the back of A[3], and p1[-1 is resolved to * (p1-1), so it is 4.

P2 = (int *) ((int) &a + 1); (int) &a The address of the array and converts it to an int type number plus 1, then the plus 1 equals two numbers added (the effect is equivalent to &a a byte backwards), the result of the addition is converted to (int *) address, so that the P2 points to this address.

Storage of current Array: 01 00 00 00 02 00 00 00 ... Because &a points to the 01 byte position, (int) The result of &a+1 is pointing to the next byte of 01, when the force type is converted to int *, then the content of the space to which the P2 points is 00 00 00 02, because it is a small-end store, so the big one is 02 00 00 00, after the output is 2000000.

<

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.