The most error-prone C language pointer

  C language pointer that is hard to say easy, but is the most error-prone place, so no matter what you do as long as you use the C pointer jump Today, however, we will go to

  Nineteen examples to give you a brief analysis about the application of the pointer, and finally there will be C language video information available to everyone more in-depth reference.

  Enter today's theme, to understand pointers, more or less some of the more complex types appear, so I'll explain how to fully understand a complex type, you want to understand the complexity class

  Type is actually very simple, a type will appear in a lot of operators, they, like ordinary expressions, have a priority, the priority and operational priority as well, so I summed up its original

  Then: From the variable name, the combination according to operator precedence, step by step analysis Let's start with the simplest type slowly began to analyze it:

  int p; // this is an ordinary integer variable

  int * p; // beginning with P, first with *, and the description is a pointer P, and then combined with the int, the type description pointer points to the content of the P type int integer data is returned. pointer

  int p [3]; // beginning with P, first with [], indicating that P is an array, then combined with the int, the array elements are described integer, the integer data P is composed of a array

  int * p [3]; // beginning with P, first with [], because of its higher priority than *, the P is an array, and then combined with the *, array elements are described pointer type, and then combined with the int, described pointer points to the type of content is an integer, and the P is an integer data pointer returned by an array of

  int (* p) [3]; // beginning with P, with the first binding *, P is described and then a pointer] combined with [(and "()" can ignore this step, just to change the priority) described pointer points to an array content is then combined with the int, the array elements are described integer. Therefore, P is a pointer to the array of the integer data

  int ** p; // P starts from the first, first with *, said P is a pointer, then combined with the * described pointer points to a pointer element is then combined with the int, indicating that the pointer points to the elements are integer data. Since the two pointers and more advanced pointer rarely used in complex types, so the back is more complex types we do not consider multi-level pointers, and most consider only a pointer.

  int p (int); // From P, first with (), indicating that P is a function, then into () in the analysis, indicating that the function has parameters of an integer variable, and then combined with the outside int , the function return value is an integer data

  Int (* p) (int); // start from P, first with the pointer, a pointer P described and then combined with (), described is a function pointer, and then combined with () in the int described function has a parameter of type int, recombined with the outermost int, return type of the function described is an integer, the P is a pointer to an integer argument and returns a pointer to a function of type integer

  int * (* p (int)) [3]; // you can skip, this type do not look too complicated from P starts, first with (), indicating that P is a function, then into () inside, and int binding, there is described a function of an integer variable parameter, and then combined with the outside * described function returns a pointer to the outermost layer, and then, first with [], returns a pointer to be described is a array, and then combined with the * described elements in the array pointer is then combined with the int, content description pointer is integer data, so P is a parameter to an integer variable data and returns a pointer to a pointer integer an array of function pointers variable composition.

  Here also is running out, our task is also so much to understand these types, other types of side dishes for us also, but we generally do not use too complex

  Miscellaneous types that would greatly reduce the readability of the program, please caution, this has been enough above types we use.

  First, the elaborate pointer

  Special pointer is a variable, the value stored in it are interpreted as a memory address. To find out a pointer to a pointer need to find out the four aspects: that

  The type of needle type, a pointer points to, or called the pointer value of the pointer points to the memory area, the pointer itself occupied memory area. Let us illustrate.

  First statement stood a few pointers to make an example:

  Example one:

  (1)int*ptr;

  (2) char * ptr;

  (3)int**ptr;

  (4)int(*ptr)[3];

  (5)int*(*ptr)[4];

  1. The type of the pointer

  From a linguistic point of view, as long as you move the pointer in the pointer declaration statement name removed, the rest is the type of the pointer. This is a pointer type itself has.

  Let us look at the various types of cases in a pointer:

  (1) int * ptr; // pointer is of type int *

  (2) char * ptr; // pointer type is char *

  (3) int ** ptr; // pointer type is int **

  (4) int (* ptr) [3]; // pointer is of type int (*) [3]

  (5) int * (* ptr) [4]; // pointer is of type int * (*) [4]

  How about? Way to find out the type of the pointer is not very simple?

  2. The pointer points to the type

  When you access the pointer points to the memory area by a pointer, the pointer points to the type of compiler determines what will be the contents of that piece of memory area as to look at.

  Syntactically, you only need to declare a pointer to a pointer declaration statement pointer name and the names of characters left * removed, the rest is the type the pointer points. E.g:

  (1) int * ptr; // pointer points to the type of int

  (2) char * ptr; // pointer points to the type is char

  (3) int ** ptr; // pointer points to the type int *

  (4) int (* ptr) [3]; // pointer points to the type int () [3]

  (5) int * (* ptr) [4]; // pointer points to the type int * () [4]

  In the arithmetic operation of the pointer, the type of the pointer points to a significant role.

  Pointer type (i.e., type of the pointer itself) and the pointer points to the type of the two concepts. When you become more familiar with C, you will find that the pointer mix in

  Together with the "Type" concept into "the type of pointer points" "pointer type" and two concepts, one of the key points proficient pointer. I read a lot of books found

  Some poorly written book, put the pointer mix these two concepts together, it looks inconsistent books, baffled.

  ---- 3. The value of a pointer or pointer is called memory area or address

  Is a numerical value of the pointer stored in the pointer itself, the value will be treated as a compiler address instead of a general numerical value. In the 32-bit program, all types

  The value of the pointer is a 32-bit integer, 32-bit program because in all memory addresses are 32 bits long. The pointer points to the memory area that is the value of the pointer from the representatives of

  A memory address, length si zeof (type pointer points) in a memory area. Later, we say that the value of a pointer is XX, is equivalent to saying that the pointer points

  The area of ​​memory led to XX address; we say that a pointer to a block of memory area, it is equivalent to saying that the value of the pointer is the first address of this memory area. Finger

  Type needle points to the memory area, and the pointer points are two completely different concepts. In one embodiment the type of the pointer points to have, but not yet the initial pointer

  Oriented, so it points to the memory area it does not exist, or is meaningless.

  Later, when confronted with a pointer, you should ask: What type of this pointer is a pointer refers to what the type is a pointer to where (the focus of attention)???

  4 pointer memory area occupied by itself

  The pointer itself accounts for how much memory? You just measure what a function sizeof (type pointer) to know. In the 32-bit platform, the pointer itself occupies 4 bytes

  length. The pointer itself occupied memory concept in a judgment pointer expression (explained later) is useful if the left value.

  Second, pointer arithmetic

  Pointer can be plus or minus an integer. Meaning as such operations typically pointer and the value of the subtraction is not the same, a unit basis. E.g:

  Example two:

  char a[20];

  int * ptr = (int *) a; // cast does not change the type of a

  ++ ptr;

  In the example above, the type is a pointer ptr int *, it points to a type of int, which is initialized to point to the integer variable a. Next, the third sentence, ptr pointer is incremented by 1, compiled

  Translation is handled this way: it adds the value of the pointer ptr sizeof (int), the 32-bit program 4 is added, because the 32-bit program, int 4 bytes. by

  The address is made in units of bytes, so the original ptr points to the address of the address of a variable increase of 4 bytes to higher addresses. Since the char is a type of word length

  Section, so that the original ptr No. 0 unit is a starting point to an array of four bytes, then a four point byte array starting from a No. 4 unit. We can use a

  And a pointer to traverse a loop array, see example:

  Example Three:

  int array[20]={0};

  int *ptr=array;

  for(i=0;i<20;i++)

  {

  (* Ptr) ++;

  ++ ptr;

  }

  This example of each value of the integer array plus 1 unit. Since each cycle ptr pointer plus one unit, so each loop can access the next element of the array.

  Look at an example:

  Example 4:

  char a[20]="You_are_a_girl";

  int * ptr = (int *) a;

  ptr + = 5;

  In this example, the 5 ptr is added, the compiler is addressed: the ptr value of the pointer by adding 5 sizeof (int), the 32-bit program is added 5 x 4 = 20.

  Since the unit address of the byte, it is now compared with the address pointed to by ptr ptr address after adding 5 points, the mobile 20 to the high byte address direction.

  In this example, did not increase ptr pointer to the array before a 5 No. 0 four byte unit, after adding 5, ptr points to the outside has a valid range of the array. although

  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, subtracting the PTR is 5, then the processing Pax

  Small differences, but the value of ptr is minus 5 by sizeof (int), the new address than the address pointed to by ptr ptr points to the original 20 is moved to the low byte address direction.

  Here let me give you another example of a misunderstanding :()

  Example 5:

  #include

  int main ()

  {

  char a[20]=" You_are_a_girl";

  char *p=a;

  char **ptr=&p;

  //printf("p=%d\n",p);

  //printf("ptr=%d\n",ptr);

  //printf("*ptr=%d\n",*ptr);

  printf("**ptr=%c\n",**ptr);

  ++ ptr;

  //printf("ptr=%d\n",ptr);

  //printf("*ptr=%d\n",*ptr);

  printf("**ptr=%c\n",**ptr);

  }

  A misunderstanding, and outputs the answer is Y o

  Misunderstanding: ptr is a pointer to a two char when performing ptr ++; time, plus a pointer will the sizeof (char), the output as a result, only a small part of this may be the result of man.

  Myth, the answer is Y output and a misunderstanding: ptr points to a char * type, when executed ptr ++; when, cause the pointer plus a sizeof (char *) (there may be some people think that

  A value of 1, it will get the answer to a misunderstanding, this value should be 4, refer to the previous content), namely & p + 4; that once the value of calculation not to point to an array fifth element that yet?

  The resulting output is not an array fifth element yet? The answer is no.

  Positive Solutions: ptr is of type char **, points to a type of type char * p is the address pointed to by an address (& p), when executed ptr ++; time, cause the pointer plus a

  sizeof (char *), namely & p + 4;? That * (& p + 4) point which, um, you ask to God, or he will tell you where it is the final output will be a random value, perhaps One

  Illegal operation.

  in conclusion:

  A pointer ptrold plus (minus) after an integer n, the result is a new ptrnew pointer, and the same type of ptrnew ptrold type, pointed ptrnew

  Type and the type ptrold points are also the same. Ptrnew ptrold value than the value of the increase (decrease) of n by sizeof (type ptrold pointed) bytes. That is to say,

  ptrnew points ptrold memory area than the memory area pointed to by n moved sizeof (type ptrold pointed) bytes to the high (low) direction address. And pointer finger

  Needle subtraction: adding two pointers can not be performed, which is an illegal operation, since after the addition, the results obtained point to the place of an unknown, and meaningless.

  Two pointers subtraction operation may be performed, but must be the same type, the array is generally used in terms of, not much to say.

  Third, the operator & * and

  Here is the address of operator &, * is the indirection operator.

  & A calculation result is a pointer, the pointer is a type of add * type, the type is a pointer points to the type, the address pointer is incorrect, that is, a address.

  * P of the operation result on the variety. In short * result p is p points to something, this thing has these characteristics: its type is the type of p points to, it occupied

  The address is the address pointed to by p.

  Six cases:

  int a=12; int b; int *p; int **ptr;

  p = & a; // & a result of a pointer type int *, is the type of point

  // int, at the address is a address.

  * P = 24; // * p results, where it is of type int, it occupied address is

  // p points to address, obviously, * p is the variable a.

  ptr = & p; // & result p is a pointer, the pointer type is p type of add *,

  // Here is int **. The pointer points to the type is p type, which

  // there is int *. This pointer points to address is the pointer p own address.

  * Ptr = & b; // * ptr is a pointer, the result of b is & pointers, and the two pointers

  // type and the type pointed to is the same, so use * ptr & b to give Fu

  // value is no problem at all.

  ** ptr = 34; // * ptr result is ptr points to something, in this case a pointer,

  // do it again this pointer * operator, the result is a variable of type int.

  Fourth, the pointer expression

  If the result of an expression is a pointer, then the expression is called pointer table style.

  Here are some examples of pointer expression:

  Example seven:

  int a,b;

  int array[10];

  you * pa;

  pa = & a; // & a is a pointer expression.

  Int ** ptr = & pa; // & pa is a pointer expression.

  * Ptr = & b; // * ptr pointer and a & b are expressions.

  pa=array;

  pa ++; // this is a pointer expression

  Example Eight:

  char *arr[20];

  char ** parr = arr; // if arr seen as a pointer, then, arr is a pointer expression

  char *str;

  str = * parr; // * parr pointer expression

  str = * (parr + 1); // * (parr + 1) is a pointer expression

  str = * (parr + 2); // * (parr + 2) is a pointer expression

  As a result of the expression is a pointer to a pointer, the pointer having a pointer expression also has four elements: the type of pointer type, the pointer points, the pointer points

  The memory area occupied by the memory pointer itself.

  Well, when the results of a pointer to a pointer expression pointer has been clearly occupy its own memory, then this is a pointer expression left value, otherwise it is not

  A value left. In seven cases, & a is not a left-value, because it does not occupy explicit memory. * Ptr is a left-value, because the * ptr pointer has occupied the memory,

  In fact, * ptr is a pointer pa, pa now have their place in memory, then * ptr course, also have their place.

  Fifth, the relationship between arrays and pointers

  In fact, the name of an array of arrays can be seen as a pointer. Facie cases:

  Example 9:

  int array[10]={0,1,2,3,4,5,6,7,8,9},value;

  value = array [0]; // also be written as: value = * array;

  value = array [3]; // also be written as: value = * (array + 3);

  value = array [4]; // also be written as: value = * (array + 4);

  In the embodiment, generally representative of the array itself array array name, type int [10], but if the array seen pointer, which points to the 0-th cell array,

  Type is the type int * is pointed, i.e., array element type int. * Array equal to 0 therefore it is no wonder. Similarly, array + 3 is a pointer to the array 3

  A pointer unit, so * (array + 3) is equal to 3. Other so on.

  Embodiment 10:

  char *str[3]={

  "Hello,thisisasample!",

  "Hi,goodmorning.",

  "Helloworld"

  };

  char s[80];

  strcpy (s, str [0]); // also be written strcpy (s, * str);

  strcpy (s, str [1]); // also be written strcpy (s, * (str + 1));

  strcpy (s, str [2]); // also be written strcpy (s, * (str + 2));

  In the embodiment, str is an array of three cells, each cell of the array is a pointer, the pointers each point to a string. Str pointer array name as a finger

  Needle, then it points to the array element No. 0, it is of type char **, which points to a type char *.

  * Str is a pointer, it is of type char *, it points to is the type of char, it points to the address of the string "Hello, thisisasample!" The first character

  Address, i.e. the address 'H' of. Note: The string is the equivalent of an array as an array of storage in memory, except that string is an array constant, unchangeable content, and can only be

  The right value. If as a pointer, he is a constant that is a pointer, the pointer is constant.

  str + 1 is a pointer to the array of the No. 1 unit, it is of type char **, which points to a type char *.

  * (Str + 1) is a pointer, it is of type char *, it points to the type of char, which points to "Hi, goodmorning." The first character 'H'

  The following summarize the problem array name of the array (the array is stored in an array) of:

  Declares an array TYPE array [n], then the name of the array array will have a double meaning:

  First, it represents the entire array, it is of type TYPE [n];

  Second, it is a constant pointer, the pointer type is TYPE *, the pointer type is TYPE, which is an array type element, this pointer points to the memory area

  No. 0 unit is an array, the pointer occupies its own separate memory area, and it is noted that the memory area occupied by the array element No. 0 are different. The pointer value can not be changed, i.e.,

  Similar array ++ expression is wrong. In a different expression array name array can play different roles. In the expression sizeof (array) in the array name array behalf

  Table array itself, so the time is measured sizeof function of the size of the entire array.

  In the expression * array, array pointer is played, so the result of this expression is the array values ​​No. 0 unit. sizeof (* array) size of the array is measured elements.

  Expression in array + n (where n = 0,1,2, .....), array pointer is played, so that the result array + n is a pointer, it is of type TYPE *, it points

  Type is TYPE, which points to the n-th cell array. Therefore, sizeof (array + n) measured by the size of a pointer type. The result is a 32-bit program 4

  Example XI:

  int array[10];

  int (*ptr)[10];

  ptr=&array;:

  In the embodiment ptr is a pointer, it is of type int (*) [10], he points to the type int [10], we use the first address of the entire array to initialize it. In the statement ptr = & array, array represents the array itself.

  In this section referred to the function sizeof (), then I'll ask, sizeof (pointer name) measured what is the size of a pointer or do their own type of pointer points to the type of size?

  The answer is the former. E.g:

  int(*ptr)[10];

  In the 32-bit program, we are:

  sizeof(int(*)[10])==4

  sizeof(int[10])==40

  sizeof(ptr)==4

  In fact, sizeof (target) measured object itself is the size of the type, rather than what other type of size.

  Six, and the structure type of relationship pointer

  You may declare a pointer to an object of type pointer structure.

  Examples twelve:

  struct MyStruct

  {

  int a;

  int b;

  int c;

  };

  struct MyStruct ss={20,30,40};

  // Declare the structure of the object ss, ss initialization and the members 20, 30 and 40.

  struct MyStruct *ptr=&ss;

  // declare a pointer to a pointer to the structure of the object ss. It is the type

  // MyStruct *, type it points to is MyStruct.

  int *pstr=(int*)&ss;

  // declare a pointer to a pointer to the structure of the object ss. But pstr and

  // It pointed to the type of ptr is different.

  How do you access the member variables ss through three pointer ptr?

  answer:

  ptr-> a; // point operator, or may have this (* ptr) .a, with the former being

  ptr->b;

  ptr->c;

  How do they access the member variables ss three pointer by pstr?

  answer:

  * Pstr; // ss visited members of a.

  * (Pstr + 1); // ss visited members of b.

  * (Pstr + 2) // visited ss members c.

  While I was in my MSVC ++ 6.0 increase over-the above code, but you know, so use pstr to access the structure members are irregular, in order to explain why

  Irregular, let us look at the various units to access an array of how pointer by: (a structure into an array)

  13 cases:

  int array[3]={35,56,37};

  int *pa=array;

  // pointer pa three by the method of cell access array array is:

  * Pa; // accessed No. 0 unit

  * (Pa + 1); // access unit No. 1

  * (Pa + 2); // access unit No. 2

  It touches on the same format as informal approach by accessing structure member pointer from the format point of view.

  All of the C / C ++ compiler when the arrangement of the cell array, each array element always stored in consecutive memory areas, there is no gap between the unit and the unit. However, in the presence of

  When each member of the structure of the object put in some kind of build environment, you may need to align word or double-word aligned or is aligned else, need to add between two adjacent members

  Several "padding bytes," which may have led to a number of bytes of the gap between various members.

  Therefore, in 12 cases, even * pstr access to the first structure of the object member variable ss, a, there is no guarantee * (pstr + 1) will be able to have access to the structure member b.

  Because there may be a number of padding bytes between a member and a member b, maybe * (pstr + 1) will just access to these padding bytes it. It also demonstrates the flexibility of the pointer.

  If your goal is to see in the end there is no padding between the various structural members bytes, hey, this is not a bad approach.

  But the right way to access structure members should be a pointer to a pointer ptr method using twelve elephant cases.

  Seven, the relationship between pointers and function

  You can declare a pointer as a pointer to a function.

  int fun1(char *,int);

  int (* pfun1) (char *, int);

  pfun1=fun1;

  int a = (* pfun1) ( "abcdefg", 7); // call the function through a function pointer.

  The pointer can be used as the function parameter. In the function call statement, it can be used as an argument a pointer expression.

  Example 14:

  int fun(char *);

  inta;

  char str[]="abcdefghijklmn";

  a=fun(str);

  int fun(char *s)

  {

  int num = 0;

  for(int i=0;;)

  {

  num+=*s;s++;

  }

  return num;

  }

  Fun function code in this example is the statistical value of each character in a string and ASCII. Said earlier, the name of the array is a pointer. In the function call, when

  Str as the argument to the parameter s, the actual value is to pass to the str s, s, and points to the address on the address pointed str consistent, but str and s are each occupied by each

  Of storage space. S in vivo function of self-operation plus 1, while not meant to be a self-str operation plus 1.

  Eight pointer type

  When we initialize a pointer to a pointer or an assignment, an assignment of the left is a pointer to the right of an assignment is a pointer expression. In the example we cited earlier

  Child, the vast majority of cases, the type and pointer type expression pointer is the same, the pointer points to the type and the type of the pointer points to an expression of the same.

  Example five:

  float f=12.3;

  float *fptr=&f;

  int *p;

  In the above example, if we want the pointer p points to a real number f, how should we do?

  It is with the following statement?

  p=&f;

  wrong. Because of the type of pointer p is int *, it points to a type of int. Results Expression & f is a pointer, the pointer type float *, it points to a type of float.

  The two are inconsistent, direct assignment method is not acceptable. At least in my MSVC ++ 6.0, a pointer assignment statement requires the same type on both sides of an assignment, pointed

  Type is also consistent with other compilers I have not tried, you can try. In order to achieve our objective, need to be "cast":

  p=(int*)&f;

  If you have a pointer p, we need to put its type and the type of pointing instead TYEP * TYPE, then the syntax is: (TYPE *) p;

  The results of such a cast is a new pointer, the new pointer type is TYPE *, it points to the type is TYPE, which points to the original address is the address pointer.

  And all the attributes of the original pointer p have not been modified. (Remember)

  If a function pointer as a parameter, then the arguments and parameters of a function call statement in the bonding process, the same type must be guaranteed, or need to cast

  Example six:

  void fun(char*);

  int a=125,b;

  fun((char*)&a);

  void fun(char*s)

  {

  charc;

  c=*(s+3);*(s+3)=*(s+0);*(s+0)=c;

  c=*(s+2);*(s+2)=*(s+1);*(s+1)=c;

  }

  Note that this is a 32-bit application, so int is four bytes, char type occupies one byte. Fun action function is to order four-byte integer to a

  A reversal. Noticed? Statements in the function call, the argument & a is a pointer to the result, it is of type int *, it points to the type is int. This type of parameter pointer

  Is a char *, it points to is the type char. Thus, during the bonding process and parameter arguments, we must convert from one type to the int * char * type.

  Combined with this example, we can be

  Imagine compiler conversion process: the compiler to construct a temporary pointer char * temp, and then perform temp = (char *) & a, then the value of the last transmission of temp

  To s. So the final result is: s type is char *, it points to is the type of char, it points to the address is a first address.

  We already know that the value of the pointer is a pointer to the address in 32-bit program, the value of the pointer is actually a 32-bit integer.

  Can it as a pointer to an integer value it as direct pointer from the following statement?:

  unsigned int a;

  TYPE * ptr; // TYPE is int, char structure type or the like type.

  a=20345686;

  ptr = 20345686; // Our aim is to make the pointer point to the address 20345686 ptr

  ptr = a; // Our aim is to make the pointer point to the address 20345686 ptr

  // compile it. It was found behind the whole two statements is wrong. So our aim can not be achieved without it, there are ways?:

  unsigned int a;

  TYPE * ptr; // TYPE is int, char structure type or the like type.

  a = N // N must represent a valid address;

  ptr = (TYPE *) a; // Oh, this it.

  Strictly speaking here (TYPE *) and the pointer type (TYPE *) is not the same. (TYPE *) here is the mean value as an unsigned integer of a to an address

  Look. The above highlights the value of a must represent a valid address, otherwise, when you use ptr, there will be an illegal operation error. Think can, in turn,

  The pointer value of the pointer address is taken out as an integer. absolutely okay. The following example demonstrates the value of a pointer taken out as an integer, and then

  This integer as an address assigned to a pointer:

  Example seventeen:

  int a=123,b;

  int *ptr=&a;

  char *str;

  b = (int) ptr; // the value of the pointer ptr is taken out as an integer.

  str = (char *) b; // this integer value as an address to the pointer str.

  Now we already know, you can put the value of the pointer as an integer taken out, you can also assign the address of an integer value as a pointer.

  Nine, pointer safety issues

  See the examples below:

  18 cases:

  char s='a';

  int *ptr;

  ptr=(int *)&s;

  * Ptr = 1298;

  Int * ptr is a pointer type of pointer, which points to type int. It points to the first address is the address of s. In the 32-bit program, s is one byte, int

  Type total of four bytes. The last statement is not only a change in the share of byte s, s and also three adjacent bytes of higher addresses also changed. This is three bytes

  Doing? Compiler only knows, writing programs people are less likely to know. Perhaps these three bytes in the data storage a very important, perhaps, it is three bytes

  The code is a good program, but because of your careless use of the pointer, the value of which is three bytes changed! This will cause the collapse of error.

  Let's look at one example:

  Cases nine:

  char a;

  int *ptr=&a;

  ++ ptr;

  * Ptr = 115;

  This example can be compiled and can execute. But see not? The first three pairs pointer ptr after adding 1 from operations, ptr point to integer variables and a neighboring high

  A memory address direction. What this storage area in that? We do not know. It is possible that it is a very important data, and may even be a code.

  The first four went so far as to write this piece in a data storage area! This is a serious mistake. Therefore, when using pointers, a programmer at heart must be very clear: I really pointer

  Pointing to where. When accessing the array pointer, it must be careful not to exceed the lower and upper bounds of the array, otherwise it will cause a similar error.

  Casts pointer: ptr1 = (TYPE *) ptr2, if sizeof (type ptr2) is greater than sizeof (ptr1 type), then use the pointer to ptr1

  When accessing the storage area pointed ptr2 is safe. If the sizeof (ptr2 type) is smaller than the sizeof (ptr1 type), then use the pointer to access ptr2 referred ptr1

  When the storage area is unsafe. As for why, the reader is combined with 18 cases and think, should understand.

  Below is a set of data two days is devoted to an analysis pointer

  http://www.makeru.com.cn/course/1861.html?s=45051

  C language programming foundation

  http://www.makeru.com.cn/course/details/2233?s=45051

  Enhance the ability of C programming

  http://www.makeru.com.cn/live/1392_1166.html?s=45051

  Tamp C language, from white to Daniel advanced road!

  http://www.makeru.com.cn/live/5413_1980.html?s=45051

  pointer

  http://www.makeru.com.cn/live/1392_238.html?s=45051

Guess you like

Origin www.cnblogs.com/923327iu/p/12613690.html