Learning C + + pointers

2010-10-20  来源:本站原创  分类:CPP  人气:202 

1, the concept of pointer pointer is a special variable that stores the value which is interpreted as a memory address. Need to figure out a pointer to a pointer to find out the four aspects: the type of the pointer, the pointer points to the type of value or a pointer called memory area pointer points, and the pointer memory area occupied by itself. Let us note, respectively.
First that stood as an example a few pointers:
Example One:
(1) int * ptr;
(2) char * ptr;
(3) int ** ptr;
(4) int (* ptr) [3];
(5) int * (* ptr) [4];
1, the pointer type from the syntax point of view, you just put a pointer to a pointer declaration statement in the name removed, the remaining part is the pointer type. This is the pointer itself has a type. Let us look at the cases of one type of each pointer:
(1) int * ptr;
/ / Pointer of type int *
(2) char * ptr;
/ / Pointer of type char *
(3) int ** ptr;
/ / Pointer of type int **
(4) int (* ptr) [3];
/ / Pointer of type int (*) [3]
(5) int * (* ptr) [4];
/ / Pointer of type int *(*)[ 4]
How? Pointer to identify the type of approach is not very simple?
2, the pointer points to the type of when you pass a pointer to access the memory area pointed to by the pointer, the pointer points to the compiler will determine the type of memory area where the contents of that piece as what to look at.
From the syntax point of view, you only need the pointer to a pointer declaration statement name and the name of the left pointer declarator * removed, the rest is the type the pointer points. For example:
(1) int * ptr;
/ / Pointer points of type int
(2) char * ptr;
/ / Pointer points of type char
(3) int ** ptr;
/ / Pointer points of type int *
(4) int (* ptr) [3];
/ / Pointer points of type int () [3]
(5) int * (* ptr) [4];
/ / Pointer points of type int * () [4]
In pointer arithmetic, the pointer points to a significant role in the type. Pointer type (that is, the type of the pointer itself) and the pointer type points to the two concepts. When you become more familiar with C, you will find pointers to mix together with the "type" concept into "pointer type" and "pointer points to the type of" two concepts is the master key pointer one.
3, the pointer value, or the name pointer points to memory area or address pointer value is the value of the pointer itself is stored, this value will be address as a compiler, rather than a general value. In the 32-bit program, all types of pointer is a 32-bit integer values, because 32-bit program memory address where all 32 bits long.
The memory area pointed to by pointer from the pointer value is represented by the memory address, the length of sizeof (pointer points to the type) of a memory area. Later, we say that a pointer value is XX, equivalent to saying that the pointer points to a memory address XX-led regional; we say that a pointer to a block of memory area to the equivalent of that piece of the pointer value is The first address of memory area.
Pointer points to memory area and the type the pointer points is two completely different concepts. In one case, the type the pointer points already, but because the pointer has not been initialized, so it points to memory area does not exist, or is meaningless.
After each encounter a pointer, you should ask: What is the type of the pointer? What type of pointer? This pointer points to where?
4, the pointer memory area occupied by itself accounts for the pointer itself how much memory? You just use the function sizeof (pointer type) measuring about to know. In the 32-bit platforms, the pointer itself occupies 4 bytes in length. Pointer to the memory occupied by the concept itself, in determining whether a pointer lvalue expression is useful.
Chapter pointer pointer arithmetic can add or subtract an integer. The significance of this operation the pointer and the usual meaning of addition and subtraction of values is not the same. For example:
Example Two:
1.chara [20];
2.int * ptr = a;
...
...
3.ptr + +;
In the example above, the pointer ptr is of type int *, it points to is of type int, which is initialized to point to the integer variable a. The next sentence of paragraph 3, the pointer ptr is added 1, the compiler is handled this way: it adds the value of the pointer ptr sizeof (int), the 32-bit program, is added 4.
As do the address is byte units, so the address pointed to by ptr from the address of a variable address to the direction of the high increase of 4 bytes. The length of the char type is a byte, so the original ptr is a pointer to an array of a first 0 unit first four bytes, this time pointing to the array a, starting from the No. 4 unit of four bytes. We can use a pointer and a loop to iterate through an array, see example:
Example Three:
intarray [20];
int * ptr = array;
...
/ / Here spent the code for the integer array assignment.
...
for (i = 0; i <20; i + +)
{
(* Ptr) + +;
ptr + +;
}
This example will be an integer value of the array plus 1 each unit. Since each loop pointer ptr will be incremented by 1, so each loop can access the array to the next unit.
Look at the examples:
Example four:
1.chara [20];
2.int * ptr = a;
...
...
3.ptr + = 5;
In this example, ptr is coupled with a 5, the compiler is handled this way: the value of the pointer ptr with 5 x sizeof (int), the 32-bit program is coupled with a 5 x 4 = 20. Because the address in bytes, so now the address pointed to by ptr compared with 5 after the address pointed to by ptr, the move to higher address 20 bytes.
In this case, did not add 5 points to an array of a pre-ptr 0 unit began the first four bytes, plus 5 after, ptr has a legitimate point to the array outside the scope of the. While this situation will be problems in the application, but the syntax is possible. It also reflects the flexibility of the pointer.
If the above example, ptr is minus 5, then the process similar, but the value of ptr is minus 5 by sizeof (int), the new ptr point to the original than the address of the address pointed to by ptr to low move the address of 20 bytes.
In summary, plus a pointer to an integer n ptrold, the result is a new pointer ptrnew, ptrnew type and ptrold the same type, ptrnew points to the type and ptrold points to the same type. ptrnew ptrold value than the value of n increased by sizeof (ptrold points to type) bytes. That is, ptrnew memory area pointed to the point than ptrold high memory area to move the n address by sizeof (ptrold points to type) bytes.
A pointer to an integer n ptrold less, the result is a new pointer ptrnew, ptrnew type and ptrold the same type, ptrnew points to the type and ptrold points to the same type. ptrnew ptrold value than the value of n decreased by sizeof (ptrold points to the type) bytes, that is, ptrnew than the memory area pointed to ptrold memory area pointed to the direction moved to the lower address by sizeof n (ptrold points to type) bytes.
Chapter III. Operators & and *
Here & is to take the address of operator, * is ... the book is called "indirect operator." & A computation result is a pointer to a pointer type is the type to add a *, the pointer points to the type of type is a pointer the address pointed to by the Well, that is a address.
* P on a wide variety of computing the results. Overall results are p * p points to something, this thing has these characteristics: its type is the type of point p, it takes the address is the address pointed to by p.
Example Five:
inta = 12;
intb;
int * p;
int ** ptr;
p = &a;
/ / & A the result is a pointer,
Type is int *, point of type int, the address is a point to address.
* P = 24;
/ / * P of the results, where it is of type int,
It takes the address is the address pointed to by p, obviously, * p is the variable a.
ptr = &p;
/ / & P the result is a pointer, the pointer type is the type to add a * p,
Here is the int **. This pointer points to the type of type p,
This is int *. The address pointer is a pointer p pointing to his own address.
* Ptr = &b;
/ / * Ptr is a pointer, & b the result is a pointer,
And the two pointers point to the type and the type is the same,
So with a & b to a * ptr assignment is no problem at all.
** Ptr = 34;
/ / * Ptr ptr points to the results of what is, here is a pointer
* Pointer to do it again in this operation, the result is a variable of type int.
Chapter pointer the final outcome of the expression of an expression if it is a pointer, then this expression is called a pointer expression. Here are some pointers expression examples:
Example VI:
inta, b;
intarray [10];
int * pa;
pa = &a;
/ / & A is a pointer expression.
int ** ptr = &pa;
/ / & Pa is a pointer expression.
* Ptr = &b;
/ / * Ptr and & b is a pointer expression.
pa = array;
pa + +;
/ / This is a pointer expression.
Example VII:
char * arr [20];
char ** parr = arr;
/ / If the pointer as if arr, arr is a pointer expression
char * str;
str =* parr;
/ / * Parr is a pointer expression
str =* (parr +1);
/ / * (Parr +1) is a pointer expression
str =* (parr +2);
/ / * (Parr +2) is a pointer to a pointer expression as the result of an expression is a pointer, it also has a pointer to the pointer with the expression of the four elements: the type of the pointer, the pointer points to the type of memory pointer area occupied by the memory pointer itself.
Well, when a pointer to the result of an expression has a clear pointer to a pointer occupies its own memory, then the pointer is an lvalue expression, otherwise it is not an lvalue.
Seven of the cases, & a is not an lvalue, because it does not take a clear memory. * Ptr is an lvalue, because the pointer * ptr has occupied the memory, in fact, * ptr is the pointer pa, since pa has been in memory has its own location, then * ptr of course, have their own position.
Chapter. The relationship between array and pointer array name is actually an array can be seen as a pointer. See next example:
Example VIII:
intarray [10] = {0,1,2,3,4,5,6,7,8,9}, value;
...
...
value = array [0];
/ / Can also be written: value =* array;
value = array [3];
/ / Can also be written: value =* (array +3);
value = array [4];
/ / Can also be written: value =* (array +4);
The above example, array name array generally representative of the array itself, the type is int [10], but if the array as a pointer, then it points to an array of 0 units of type int *, points to an array of type the type of unit that is int. So * array is not equal to 0 to strange. Similarly, array +3 is a pointer to an array of pointers to the first three units, so the * (array +3) is equal to 3. Other and so on.
Example IX:
char * str [3] =
{
"Hello, thisisasample!",
"Hi, goodmorning.",
"Helloworld"
};
chars [80];
strcpy (s, str [0]);
/ / Can also be written strcpy (s, * str);
strcpy (s, str [1]);
/ / Can also be written strcpy (s, * (str +1));
strcpy (s, str [2]);
/ / Can also be written strcpy (s, * (str +2));
On the case, str is an array of three units, each unit of the array is a pointer, the pointer pointing to a string. The pointer array name str as a pointer, then it points to the first array element 0, it is of type char **, it points to is of type char *.
* Str is a pointer, it is of type char *, it points to is of type char, it points to address is the string "Hello, thisisasample!" The first character of the address, that 'H' in the address. str +1 is a pointer to an array of No. 1 unit, it is of type char **, it points to is of type char *.
* (Str +1) is also a pointer, it is of type char *, it points to is of type char, it points to "Hi, goodmorning." The first character 'H', and so on.
The following summary of the array and the array name issue. Declares an array TYPEarray [n], the array name of the array will have a double meaning: First, it represents the entire array, it is of type TYPE [n]; Second, it is a pointer to the type TYPE *, the pointer of type TYPE, which is the array element type, the pointer memory area is the array unit number 0, the pointer to hold their own separate memory area, pay attention to it and the array occupied by the first unit 0 memory area is different.
The value of the pointer can not be changed, that is similar to the array + + expression is wrong. Expression in different array name array can play different roles.
In the expression sizeof (array), the array name array representative of the array itself, so the sizeof function of time measured the size of the entire array. In the expression * array, array is a pointer to play, so this result is an array of the first expression unit value of 0. sizeof (* array) unit of measure is the size of the array.
Expression array + n (where n = 0,1,2 ,....。) in, array is a pointer to play, so the array + n the result is a pointer, it is of type TYPE *, it points to the type of is a TYPE, it points to an array of n-number units. Therefore, sizeof (array + n) measured the size of a pointer type.
Example X:
intarray [10];
int (* ptr) [10];
ptr = &array;
The example ptr is a pointer, it is of type int (*) [10], he pointed to the type is int [10], we first address the entire array to initialize it. In the statement ptr = & array in, array representative of the array itself.
Functions mentioned in this section sizeof (), then I'll ask, sizeof (pointer name) to detect is it a pointer to the size of their type or the pointer points to what type of size? The answer is the former. For example:
int (* ptr) [10];
In the 32-bit program, are:
sizeof (int (*) [10]) == 4
sizeof (int [10]) == 40
sizeof (ptr) == 4
In fact, sizeof (object) are measured the size of the type of the object itself, not the other what kind of size.

相关文章
  • Learning C + + pointers 2010-10-20

    1, the concept of pointer pointer is a special variable that stores the value which is interpreted as a memory address. Need to figure out a pointer to a pointer to find out the four aspects: the type of the pointer, the pointer points to the type of

  • 一本介绍C指针的书--指针的类型及数组2.2 2012-08-04

    Earlier when discussing the term "lvalue" I cited K&R-2 where it stated: "An object is a named region of storage; an lvalue is an expression referring to an object". This raises an interesting problem. Since my_array is a named reg

  • C pointers Learning Summary 2010-11-11

    1, the pointer to a function definition of variables Return data type (* pointer variable name) (parameter list) Such as: int (* p) (int, int); also function int max (int a, int b); p = max will first address the function assigned to p, so that you c

  • rookie learning flash: Loading library movie clip 2010-03-30

    The patient was a Flash AS3.0 rookie learning to fly series of tutorials, in this Flash AS3.0 tutorial we will learn a brief example of how the eagle flew load library movie clip will be made into animation, I hope to give my friends be helpful ~ ~ O

  • Classification Algorithm Learning Summary - Decision Tree 2010-08-30

    table (font-size: 10pt;) Classification Algorithm Learning Summary - Decision Tree Because the needs of the recent work on classification algorithm based on machine learning more interested in Sort of things is a more common applications, including e

  • Embedded Linux learning ideas and reference materials (ARM + LINUX line) 2010-11-08

    Embedded Linux learning ideas and reference materials (ARM + LINUX line) Embedded talent demand (1) embedded application engineers: mainly for computers, software engineering background. Its based on a platform, such as embedded Linux, Vxworks, Symbi

  • Boost and STL Learning Sourcebook 2011-01-06

    from http://blog.csdn.net/k2eats/archive/2008/09/06/2892948.aspx Quote Boost Library Study Guide Boost Chinese site After Boost library is a tempered, portable, providing the source code of C + + library, the library as a standard backup, is the C +

  • Experiential learning 2011-07-29

    Prior statement, which I did not write, but we might look smile. I have always believed that for a beginner, IT sector, the wave can not catch up with the technology, but can not afford to go to catch up. I always saw my DDMM who threw the textbooks,

  • linux embedded learning route 2011-08-17

    Wrote ARM + LINUX line, the main embedded Linux operating system and application software development on target: (1) master the mainstream structure and principle of the embedded microprocessor (initially arm9) (2) must master an embedded operating s

  • Started learning jQuery 2009-06-22

    Reprint: OneChen's Blog Read some basic knowledge about the Jquery. Posted about sharing. A lot of good articles online. Study hard in ~ With the rise of JavaScript, JavaScript libraries have a series of flourishes. From the Early Prototype, Dojo 2006 jQu

  • Second EXTJS learning 2009-07-07

    Second EXTJS learning

  • Learning Notes Series ROR a Windows XP installation configuration of the environment (2) 2009-07-09

    Recently concluded a project , Idle boredom , So prepare yourself to learn about ROR charge your energy , ROR in JavaEye see above for a long time , But the comparison has been busy as a Java project , So no more attention to , Robbin is inadvertentl

  • Basic knowledge of learning 2009-07-10

    Learning to learn Java first line j2se To learn j2ee must first learn j2se, just beginning to learn is not recommended to use j2se first IDE, then gradually transition to the use of the IDE development, after all, why use it conveniently. J2se study

  • Summary of learning J2EE 2009-07-22

    Summary of learning J2EE J2EE learning for some time, today javaEye sum up, when we look back to learn some things themselves, while summing up the work must be under the hope of learning java from a friend there. Technology-related (and more time to

  • JAVA materials (recommended novice learning) 2009-07-26

    The contents of a very comprehensive JAVA materials, from J2SE to J2EE, easy-to-digest and explain, very suitable for beginners Contents Chapter I: Java started 1 Learning Objective 1 Java History 2 Java Technical Overview 3 3 advantages of Java tech

  • learning java Spring Records 2009-08-12

    Recently, there have been in learning spring, the spring that is too powerful features, and now summarized by the following school today, I feel a little less learned, not Have learned how to spring, it is estimated to redouble their efforts would al

  • Learning Center MLDN the MVC design pattern 2009-08-23

    Today, access to the MVC design pattern, the Servelt, jsp, java bean relationship between a number of clear, but the development code so I felt quite trouble with the previous design study of the not very familiar with the search of the next-line inf

  • The road to learning JAVA 2009-08-26

    The road to learning JAVA

  • Step-by-step learning Ruby (13): Ruby module 2009-09-23

    Class is subclass of Module, is a special form of modules, this can look at my article Step-by-step learning Ruby (5): Class, Module, Object, Kernel relationship . We know that the top of the class is Object, then the top of the module is the Kernel

  • Extjs Chinese learning website 2009-10-28

    EXT I think learning to develop the best approach is to develop in the real before, mastering advanced JavaScript knowledge, Just as fish swim in the water with ease, like on the JS. JavaScript can create their own class, understand the category prot