Mandatory type conversion appreciated pointer


A few days ago in judgment   " value pointer points to the same two variables may be different  ,"   these words, found himself on the pointer type concepts just remember conclusion. So access to some information, some of it related to the record pointer type conversion and pointer type of knowledge.
 

 Some used

Before you begin, let's review some of the information will be used.

1. address, byte, bit

  Bit (bit) computer is the smallest unit of data. Every state can only be 0 or 1.

  Byte (Byte) is a unit of measurement for storage capacity, each byte consists of 8 bits (1Byte = 8bit).

  It can be understood as an address in memory, each byte (Byte) number.

  In memory of their relationship can be compared, the memory is a building, a byte (Byte) is building each layer, the address is the floor number, digit (bit) of each layer is in the room, each layer has 8 rooms.

   

 

2. Variable memory

  The compiler type variable, an application in the memory space. For example, 32-bit and 64-bit application to type int 4 bytes of space, it is understood to apply a floor 4 for the compiler, as a "work area."

3. pointer variable

  It refers to the address pointer in the program data memory. In which c language, allows a variable to store the pointer, this pointer variable called variable.

 

Pointer variable effect type

  

1 int a;
2 int *p;
3 p = &a;
4 printf("%p %d\n",p,*p);

 

  In the above procedure, "&" operator removed variables a first address in memory space, and then through the "*" operator remove the first address data memory space is located.

  We mentioned earlier, memory storage variable, by a plurality of bytes. And the pointer variable in the first case only know the address (byte address) of a memory area to find. It is how to do? Let's look declare a pointer variable.

  We declare a pointer variable, based on the type of the variable to point to it, declare the corresponding type, for example:

1     int a;
2     long b;
3     char c;
4 
5     int *pa = &a;
6     long *pb = &b;
7     char *pc = &c;

  No matter what type of pointer variable, the stored value is the address (an int). So what is the role of different types of statements? The answer is that the number of bytes in each movement of the pointer in a predetermined memory.

  Defined, for example "int * pa = & a", when the value, int type 4 bytes, the pointer is moved from the start of the first address, reads 4 bytes. Similarly, short type 2 bytes, 2 bytes of the pointer moves. By declaring a pointer type, the pointer tells how many bytes each move, to get the value of the variable.

 

Value pointer points to the same two variables may be different

  "Two same value tag", mean that the two pointers point to the same first address variables. However, if a different type of pointer variables, because of the different number of bytes pointer movement, it is possible to read different data.

  To achieve the different types of pointer variables point to the same address, pointer type.

1     short a = 1;
2     short *p1 = &a;
3     int *p2 = (int *)p1;
4     printf("%d %d",*p1,*p2);

   The above example will read each movement of a 2-byte pointer variable short type, each read into a 4-byte int type pointer variable.

   Next, we pass pointer type, with the same first address, a different value taken.

#Include. 1 <stdio.h> 
 2 int main () 
 . 3 { 
 . 4 Short C [2]; // application equivalent to two consecutive memory space, each space 2 bytes 
 5 c [0] = 1; / / is the first short space assigned. 1 
 . 6 C [. 1] =. 1; // is assigned to the second short space. 1 
 . 7 short C * = P1; P1 point C // [] first address 
 8 int * p2 = (int *) p1; // p2 point C [] the first address, and cast to type int 
 . 9 
10 the printf ( "point P1:% p \ np2 point: P% \ n-", P1, P2);       
. 11 the printf ( "p1 removed:% d \ np2 taken: D% \ n-", P1 *, P2 *); 
12 is return 0; 
13 is}

  The corresponding results are: 

    p1 point: 000000000062FE30
    P2 point: 000000000062FE30
    p1 removed:. 1
    P2 taken: 65537

  

    

    The binary conversion too, is 10000000000000001 65537. Whereby the front cast verify read pointer 2 bytes, 4 bytes read after transformation. The same two pointer to the first address, but read different results.

Guess you like

Origin www.cnblogs.com/al-fajr/p/11615413.html
Recommended