[C language] Pointer basics - the difference between pointer types

This article will introduce pointers and describe them in detail.

The difference between pointer types is in the second half

1. What is a pointer

       The pointer is also the memory address. The pointer variable is a variable used to store the memory address . Under the same CPU architecture, the length of the storage unit occupied by different types of pointer variables is the same (for example, in the X86 environment, the size of the pointer variable is 4 bytes), and the variables that store data occupy different lengths of storage space depending on the type of data. With the pointer, not only the data itself, but also the address of the variable storing the data can be operated.

2. How to operate the pointer

2.1. Creation of pointers

       The declarative formula of the pointer variable adds "*" on the basis of the C data type, which tells the compiler that the variable is a pointer variable, and the operation is shown in the following figure:

int main()
{
	int a = 0;
	int* b;
	return 0;
}

        The above code declares an integer variable a and creates an integer pointer b.

2.2, the use of pointers

       The pointer variable is used to store the address , the address stored in the pointer, which needs to mention the address symbol - &, using & will get the address of the variable, the operation is as follows:

int main()
{
	int a = 0;
	int* b;
	b = &a;
	return 0;
}

        b=&a, this operation stores the address of a in b, at this time, the address of a is 001df914, after b=&a operation, the storage in b is 001df914, as shown in the figure below:

        At this time, the address of a is stored in b, so that the variable a can be operated by operating the address stored in b, how to operate a through b? This requires the use of the dereference symbol "*". The * pointer variable can directly access the data pointing to the address stored in the pointer variable. By using *, the pointer variable can be dereferenced to operate the variable a, which is equivalent to * b is the same as a. As shown below:

2.3, the difference between pointer types

        In a 32-bit environment, pointer variables are all four bytes; in a 64-bit environment, pointer variables are all eight bytes.

Although the size of pointer variables is the same in the same environment, the properties of different pointer types are very different.

difference:

1> Different types of pointer dereference spaces have different sizes

Integer pointer dereference operation 4 bytes

Character type pointer dereference operation 1 byte

Examples are as follows:

int main()
{
	int* a;
	char* b;
	int num = 0x12345678;
	a = #
	b = a;
	*b = 0;
	printf("%x", *a);
	return 0;
}

The final output result is 1200 (hexadecimal)

This is because num=12345600 (hexadecimal) is converted to binary

num=0001 0010 0011 0100 0101 0110 0111 1000 (binary)

A hexadecimal system is represented by 4 binary numbers, which means that a hexadecimal system needs 4 bits in size

Give the address of num to a, and point to the following figure:

The lowest bit of the num address is 0x008ff734 

a points to the lowest bit of the num address. At this time, the address stored in a is 0x008ff734, and then the address stored in a is given to b. At this time, the address stored in b is also 0x008ff734, and then b is dereferenced and assigned a value of 0, because b is a character pointer, and only operates on one byte. 7 and 8 are hexadecimal numbers, so 7 and 8 are in one byte. At this time, *b=0, which will overwrite 7 and 8 with 0 ,As shown below:

 So in the end it will output 1234500

2> Different types of pointers get different addresses after +1 operation

For example: after the +1 operation is performed on the integer pointer, the address in the pointer will be increased by 4 bytes; after the +1 operation is performed on the character pointer, the address in the pointer will be increased by 1 byte.

Examples are as follows:

int main()
{
	int* a;
	char* b;
	int num = 0x12345678;
	a = #
	b = a;
	a = a + 1;
	b = b + 1;
	printf("num地址: %x\n", &num);
	printf("a: %x\n", a);
	printf("b: %x\n", b);
	return 0;
}

If the address of num is 0x0113f8e4

Then the output is:

a:0x0113f8e8

b:0x0113f8e5

The reasons are as follows:

Before a=a+1 and b=b+1, both a and b store 0x0113f8e4, that is, both a and b point to the same space, when a=a+1 and b=b+1 operate Finally, since a is an integer pointer and b is a character pointer, the address in a after a=a+1 is 0113f8e8, and the address stored in b after b=b+1 is 0113f8e4. as follows:

Supongo que te gusta

Origin blog.csdn.net/peng_lv/article/details/127933170
Recomendado
Clasificación