Some use C language pointer


Pointer is the soul of the C language, where the essence. Pointer powerful and dangerous, is a great tool to good use, used properly is a big
potential hazards. It has a powerful pointer and dangerous characteristics, plus pointer more difficult, a lot of people with bad, so the more packaging
a high level of language, the more there is no pointer "present." Such as JAVA, Python, is to hide the pointer to go, do not let the user touches, but many functions
in essence, is to use a pointer, but is a good package, hidden from the user.

If the pointer learn better, so much easier when programming will cope with the moves played.

Many people find it difficult pointer, it is not true, as long as the understanding of the essence of pointers, more common, more training, that is, 1 + 1 = 2 things.


The essence of the pointer is also very simple: the C language, a pointer is a data type used to store various types of addresses, which address can be found by the address
value of the cell, so that the pointer. In simple terms, the pointer is the address of the store.

 

Pointer powerful reason: To run the program must be loaded into memory, so the instruction and the programs you want to run to the
use of data is a pointer can get to, with the pointer by the memory address you can control the program running processes and data used
and can do whatever they want, of course, in general a pointer can access all legal address, as to how to access illegal addresses, that is another topic.

Now take a look at some uses of pointers.
In the C language, the size of any type of pointer is 4 bytes, because the address is stored.
1.
  int * p; // definition of a pointer to an integer, the integer address data can be stored
  int a =. 3;
  p = & a; (p stored on the address a, that is a point to p, p * it is equal to 3)

2.

  char * p = "abcdef"; // character pointers, p is stored in the first character (a) address, * p == a;

3.
  int A1 [10]; // integer array, A1 is an array to store integer (int) data
  int * p1 = a1; // array of pointers, p1 is a pointer address storing integer data

  int * a2 [10]; // pointer array, a2 is an array, a pointer is stored in an integer (int *) Data

  int (* p2) [10]; // pointer array, p2 is a pointer, it is stored in the address storing integer data array 10 of

4.
  void fun(int,int)
  {

  }

  void (* p1) (int, int); // function pointers, p1 is a pointer, is stored in a void return value, parameters for the address of the function of two int
  p1 = fun; // function is the function name . the address
  p1 ();

  Common usage: typedef void (* pointer) (int, int); // pointer to a custom type, the type can be defined return value represents a void, two int type pointer parameter is a function


  pointer p2; // p2 is a function pointer
  P2 = Fun;
  P2 ();

5. The
  void (* A [10]) (int); // pointer to function, a is an array, because the '[' priority than the '*' large, and the first A '[' binding, the data is stored Returns is void, int parameter of a function pointer,

6. The
  void (* (* P) [10]) (); // pointer array, p is a pointer (void return value storage 10, no argument function pointer data) address array

7.
  two pointers: a pointer stored in the data representing itself a pointer.

  char ** p; // Pointer two

  char * str [2] = { "abc", "defg"]; // array of strings

  p = str; // p is a two pointer address storage array, * p is a pointer is the address of the first character of the first string, ** p is the character 'h'


8.

  Pointer can add and subtract

  int a [10];
  assuming
  a [0] Address: 1000 (decimal)
  A [. 1] Address: 1004 // int four bytes as in the C language
  addresses a [2] is: 1008

  int * p = a; // p is stored in a [0] address 1000 because the name is the address of the first array element of the array

  p ++; // p is not incremented by 1 at this time becomes 1001, p at this time should be 1004, the pointer subtraction is the number of bytes occupied by data type addition or subtraction
           // int as 4 bytes, so p ++ 1004 point to the next element. Is a [1]


9.

  There are many pointer usage, some are complex, but the essence is the same, as to how the power of hands with them, it is up to you
  on demand played a program at the time of writing.

  Again, like something familiar, to be more common, more training, more like.

 

Guess you like

Origin www.cnblogs.com/duichoumian/p/11106548.html