Tag: pointer
1. pointer constants and constant pointers
Int A = 10;
Int * const p1 = &;
Const int * P2 = &;
P1 is a constant that cannot change the address, that is, a pointer constant, but can modify the content it points.
P2 is a pointer to a constant, that is, a constant pointer. The address content it points to cannot be modified, but it can indeed be modified.
2. pointer operation
#include<stdio.h>int main(){int *ip1,*ip2,ivalue;char *cp1,*cp2,cvalue;ip1 = (int*)0x500;ip2 = (int*)0x518;ivalue = ip2 - ip1;cp1 = (char*)0x500;cp2 = (char*)0x518;cvalue = cp2 - cp1;printf ("ivalue=%d cvalue=%d\n",ivalue,cvalue);return 0;}
Running result: ivalue = 6 cvalue = 24
The value of a pointer is related to its type. The two addresses are also 24 bytes different, and the result of integer pointer Subtraction is 24/4 = 6; the result of character pointer Subtraction is 24/1 = 24;
3. pointers and Arrays
#include<stdio.h>int main(){int a[2][3][4] = {{{101,102,103,104},{111,112,113,114},{121,122,123,124}},{{201,202,203,204},{211,212,213,214},{221,222,223,224}}};printf("%d\t%d\n",**(a[0]+2),sizeof(a));return 0;}
Output result: 121 96
Analysis: For an array A, * A is equivalent to a [0], and * (a + 2) is equivalent to a [2]. Likewise, for multi-dimensional arrays * A [0] is equivalent to a [0] [0], * (a [0] + 2) is equivalent to a [0] [2], ** (A [0] + 2) is equivalent to * A [0] [2] and is equivalent to a [0] [2] [0].
Sizeof (a) calculates the memory space occupied by array A: (2*3*4) * 4 byte = 96 byte.
Pointer, pointer, pointer...