Pointer arithmetic and cast types

pointer
a. pointer == address
b. The address to get any variable is: (take address character) & variable name
c. * pointer variable name, then indirectly access the variable pointed to by the pointer

d. Assignment of the same type

operation
1. Pointer + number //Need to be adjusted, the weight of the adjustment (grid size) is sizeof (the pointer removes the * sign, which is equivalent to dereference)
   For example, the sizeof after adjustment of *int is 4, and the sizeof of **double after adjustment is 4 (because the size of all pointers is 4, the second-level pointer without a * sign is still the first-level pointer)

(1) Add a cell: p++ (a cell is a few bytes depending on the type of the pointer, for example, on a 32-bit platform, int is 4 bytes, float is 4 bytes, double is 8 bytes)

#include <stdio.h> //Add a cell to replace the 1. of the original a[0] and the 2 of a[1] with 10 and 20 respectively
int main()
{
 int arr[]={1,2,3,4};
 int *p = arr;
      *p = 10;
        p++;
      *p = 20;
  printf("%d,%d\n",arr[0],arr[1]);
}

(2) Add a byte: p=(int*)((int)p+1) But it is usually not used, because it is troublesome after all


2. Pointer-Number //Operation is the same as addition

3. Pointer - pointer //The interval is the number of cells, not the number of bytes, the example is the first cast of the cast
    Method: 1. Calculate the number of bytes
              2. Divide by the adjusted weight
    Summary: All operations of the pointer must be adjusted, and the weight of the adjustment is sizeof (the pointer removes a * sign)

Coercion:

1. Decimal subtraction (addition) method output. The following example uses subtraction as an example

#include <stdio.h>
int main()
{
 int arr[]={0,1,2,3,4,5,6,7,8,9};
 int *q = &arr[1];
 int *p= &arr[9];
 printf("%d\n",pq); //No conversion type directly calculates 9-1=8
 printf("%d\n",qp); //Same as above 1-9= -8
 printf("%d \n",(short*)p-(short*)q); //The number of int bytes is 4, a total of 4*8=32 bytes, divided by the weight of the adjusted pointer short is 2, and the final value is 16
 printf("%d\n",(double*)p-(double*)q); //Similarly, the weight divided by double is 8, and the final result is 4
 printf("%d\n",(int* **)p-(int***)q); //int*** after removing a * sign, it is a secondary pointer, the weight is 4, and the final value is 8
 printf("%d\n",(char* *)p-(char**)q); //char** is a first-level pointer after removing a * sign, with a weight of 4, and finally 8
 printf("%d\n",(long)p-( long)q); //long is not a pointer and cannot be dereferenced to remove a *. The difference is still 32 bytes
}
2. Hexadecimal subtraction output
#include <stdio.h>
int main()
{
 int *p = (int *) 0x2010; // borrow one bit forward is 16
 printf("%x\n",p-2); // directly calculate p-2*4=2008 without conversion type
 printf("%x\n",(float*)p-2); // pointer f After dereference float is 4 bytes, so it is also 2008
 printf("%x\n",(short*)p-2); // Similarly, the number of short bytes is 2, a total of 4 bytes, in decimal 12 is hexadecimal c, the final value is 200c
 printf("%x\n",(double*)p-2); // Similarly, the number of double bytes is 8, a total of 16 bytes, and the final value is 2000
 printf("%x\n",(unsigned short *)p-2); // Similarly, the unsigned short byte is 2, a total of 4 bytes, and the final value is 200c
 printf("%x\n",( char*)p-2); // Similarly char is 1 byte, a total of 2 bytes, then the result is 200e
 printf("%x\n",(long*)p-2); // Similarly long is 4 bytes, a total of 8 bytes, the result is 2008
 printf("%x\n",(unsigned long long)p-2); // unsigned long long constant, not a pointer, so it is still 2 bytes, the result is 200e
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325652032&siteId=291194637