c language pointer preliminary analysis

1.c language type three types:
(1) built-in type: char, short, int, double, etc.
(2) Custom Type: struct, union (class c ++) and the like
(3) pointer type: int *, char *, float *, etc.
Whether built-in type or from a pointer type defined type has a corresponding
first to look at the basic data types and basic types of pointer

2. The basic data and the amount of space pointer problem
(1) run in the following code vc:
the printf ( "% D \ n-", the sizeof (char));
the printf ( "% D \ n-", the sizeof (Short ));
the printf ( "% D \ n-", the sizeof (int));
the printf ( "% D \ n-", the sizeof (a float));
the printf ( "% D \ n-", the sizeof (Double));    

The output of the 32-bit windows environment:
. 1
2
. 4
. 4
. 8

(2) to first understand the meaning of the 32-bit environment:
hardware 32bit (processor 32), the operating system is 32bit, 32bit C compiler
so that different environment the results may vary, sizeof () of the basic data types are determined according to the above process
(3) the difference between the pointer and the type of basic types:
the printf ( "% D \ n-", the sizeof (char *));
the printf ( "% D \ n-", the sizeof (Short *));                            
the printf ( "% D \ n-", the sizeof (int *));
the printf ( "% D \ n-", the sizeof (a float *));
the printf ( " % d \ n ", sizeof ( double *));

The output is:
4
4
4
4
4
found that no matter what type of pointer, they determined the type of sizeof size is 4,
beginners will feel very strange, let's look at what are the reasons:
a pointer is itself a variable, so we call a pointer defined data pointer variable, the variable is a pointer value, this value is typically expressed in hexadecimal memory, e.g. 0x0019ff3c, he eight hexadecimal 16, so that four bytes in size, sizeof a pointer is 4 bytes in size it

3. further explore the pointer
(1) the relationship between the pointer and the variable
int A = 10;
int = 20 is B;
int * P = NULL;
int ** S = NULL;
P = & A;
S = & P;

p = 100 *;
* S = & b;
This defines a pointer and a pointer to a pointer p s, so that they are initialized to NULL pointer (which is a strategy for security).
Then p = & a representation of the pointer p points to variable a, on the condition that must be of type int * p, so he can point to a variable of type int a.
s = & p s represents a pointer pointing to a (pointer) variables p, so * s, you must be a type int * type, he can point to type int * (pointer) variable p

(2) the determinants of type pointer
from the above we know that a pointer can point to a variable type must be the same between them.
Value problem (3) pointers
a pointer has three values and his associated:
p: p = the value of the pointer p after a & stored a the address, the operation of p is equal to the address operation on a, so p changed a also It will be changed.
P = & performed after the operation of a, a value of p equal to the address of, e.g. 0x0019ff2c, * p = a, their value 10
* p: p * = 100 is quite within the meaning of variable p (i.e. a) re-assignment, mentioned above, will also be a change (p = & a), so * p = a = 100 (preceded by * p = a = 10).
& p: s = & p s behalf pointer storing an address pointer p, p empathy s corresponds to the operation of the address, i.e. the value of s * p value (in this case a = 10, ** s = 10 ) .
* s = & b, in essence, the pointer p to point B, this time is equivalent to p = & b, that is the change point p, this time ** s = * p = b = 20, a constant value because no longer point to a p a.
Similarly available int *** q (point to a pointer to a pointer, three pointer) can point to int ** s, i.e. q = & s, the operation described above, the case s = & p, but whether the pointer is a pointer several stages , we can only pointer variables (e.g., q) operation.

(4) substantial pointer
nature of the pointer variable is a data store address pointer must also have type, a pointer is a type of variable type referred.
The essence of a type of the predetermined variable is in memory for a variable byte size and representation in memory. This determines the significance of the type of pointer.
Since the variable address pointer is stored, so that changing the value of the pointer points to, it is equivalent to changing the indirect variable.
Value of the pointer is always a memory address, a pointer to change, that is, to re-assignment to the pointer, and this time the original pointer variable pointed it does not matter.
Given a pointer variable value (memory value) and a type of his theory to control any one of the data memory area (in the case os permitted).

4.const details and pointers used
int A = 10;
int = 20 is B;

int * = & the p-const A; // int const * the p-
* the p-= 20; // error
p = & a; // correct
int * const p = & a; // const * left or right in the
const int * const p = & a;
* P = 20 is;
P = & B;
the printf ( "% D \ n-", * P);
(. 1) const int * P (int const * P) commonly referred to as a constant pointer (pointer constant) * const value of p, p * is blocked corresponding to read-only (readonly),
this time the value of p * can not be changed, but can change point p (p = & b). However, the modification a (p pointed object) value, * p is changed, const pointer only affect, does not affect the variable is pointed.
(2) int * const p referred pointer constant (and the corresponding pointer variables), this time to p itself is blocked const, p is read-only, can not change the value of the pointer p (the address value),
this time may vary * p value pointed to indirectly modify the value of the variable, but can not change the point p. Similar to the pointer p and the address of a bound variable.
(3) const int * const p (const int const * p) is usually called constant pointer constant (constant pointer pointing to a constant), taking into account the above two properties.
Neither modified the pointer to p, * p can not be changed by the value of the referent. Compare seemingly to no avail.
In practice, if we only need the value of this variable, without the need to modify the operation, this can protect the security of our code, so as to avoid modifying the return value of the function reference.
(4) general use them, whether we should have a const variable is determined according to whether the pointer points to the need for const

Published an original article · won praise 0 · Views 25

Guess you like

Origin blog.csdn.net/qq_37291326/article/details/105098132