[C language-the role of different types of pointers in arrays]

1. Pointers of different types occupy the same space:

There are many types of pointers. When calculating the space occupied by different types of pointers, we found that they all occupy 4 bytes.

	char* p1;
	int* p2;
	short* p3;
	float* p4;
	printf("%d\n",sizeof(p1));//4
	printf("%d\n", sizeof(p2));//4
	printf("%d\n", sizeof(p3));//4
	printf("%d\n", sizeof(p4));//4


2. The type of pointer determines the scope of dereference:

First define a variable: int a = 0x11223344; (Supplement: 4-digit binary represents a hexadecimal digit. 0x11223344 has a total of 32-digit binary, which can be regarded as 4 bytes, and the variable defined by the int type is 4 bytes)

int a = 0x11223344;

It is stored in memory as follows:

 The low-order byte of data is stored in the low-order address of memory

 The high-order byte of data is stored in the high-order address of memory (little-endian mode)

[Char type pointer] Then define a char type pointer variable pointing to variable a; and reassign the dereference of the pointer.

char* pa1 = &a;
*pa1 = 0x88;

Result output:

 In other words, using a char type pointer to reassign variable a only changes the first byte, leaving the remaining bytes unchanged. In other words, the char type pointer dereferences a byte.

[int type pointer] Define an int type pointer variable, which also points to a, and reassigns the value.

	int * pa2 = &a;
	*pa2 = 0x55667788;

 At this time, all 4 bytes are transformed, which means that 4 bytes can be dereferenced using an int-type pointer.


3. The type of pointer determines the step size of the pointer:

[char type pointer]

int c[10] = { 0 };
	char* p = c; 
	int i = 0;
	for (i = 0;i<10;i++)
	{
		*(p + i) = 1;
	}

Result: As you can see in the picture above, 40 bytes of memory have been allocated.

 It can be seen that the char pointer points to each byte continuously, first points to the first byte, then assigns a value, then points to the next byte, and then assigns a value...

[int type pointer] Define an int type pointer variable, and the for loop reassigns the elements in the array

int c[10] = { 0 };
	int* p = c; 
	int i = 0;
	for (i = 0;i<10;i++)
	{
		*(p + i) = 1;
	}

Result: You can see that 40 bytes of memory have been allocated

Assignment result:

It can be seen that the pointer points to the first byte and assigns values ​​to the 4 consecutive bytes starting from the current address. Then the pointer points to the 5th byte and assigns values ​​to the next 4 consecutive bytes. (It does not mean that only the red 01 is changed as shown below. In fact, the assignment process is to change

01 00 00 00 is stored in memory)

For a clearer display:

I initialized the c[] array as follows and assigned the array element a value of 5 in the loop

	int c[10] = { 0x11111111,0x22222222,0x33333333,0x44444444 };
	int* p = c;  
	int i = 0;
	for (i = 0;i<10;i++)
	{
		*(p + i) = 5;
	}

Result: The first 4 bytes are assigned values ​​of 0x11111111, 0x22222222, 0x33333333, 0x44444444 respectively, and the following defaults to 0;

 Then enter the first for loop assignment:

That is, the int pointer points to the first address, but the dereference range is 4 consecutive bytes.

 

Guess you like

Origin blog.csdn.net/ggbb_4/article/details/129326918