Basics of pointers

     Personal homepage: Click me to enter the homepage

Column classification: C language, elementary       C language programming————KTV        C language games

Welcome everyone to like, comment, and collect.

Work hard together and go to Dachang together.

Table of contents

1. Type of pointer

1.1int*type

1.2char* type

1.3short* type

1.4double* type

1.5 Summary

2. Pointer addition and subtraction of integers

2.1 Pointer plus integer

 2.2 Pointer minus integer

 2.3 Summary


         Today I bring you the basic part of time pointers. For the application of pointers in arrays, pointers, and the application of pointers in arrays, you can read my other blog link as follows: https://blog.csdn.net /Infernal_Puppet/article/details/131295654Today I mainly bring about the types of pointers, pointers plus and minus integers, pointers minus pointers, and secondary pointers. I hope you can learn something.

1. Type of pointer

        The types of pointers can be divided into four categories: int*, char*, short*, and double*. These pointers occupy 8 bytes in 64-bit and 4 bytes in 32-bit. For details, see above Link; for needle-only types. I recently learned something very interesting. We first define a hexadecimal number a as 0x11223344, and then define pointers to int*, char*, short*, and double* respectively and then modify a. What do you think the answer is? What does it look like? Let's move on.

1.1int*type

code show as below

#include<stdio.h>
int main()
{
	int a = 0x11223344;
	int* p =  & a;
	*p = 0;
	return 0;
}

Entering debugging we can see

We set *p=0 and it modifies 4 bytes.

1.2char* type

code show as below

#include<stdio.h>
int main()
{
	int a = 0x11223344;
	char* p = (char*)&a;
	*p = 0;
	return 0;
}

Here we need to perform type coercion. The debugging results are as follows

 We see that it only modified 1 byte;

1.3short* type

code show as below

#include<stdio.h>
int main()
{
	int a = 0x11223344;
	short* p = (short*)&a;
	*p = 0;
	return 0;
}

The debugging results are as follows

 We see that it modified 2 bytes

1.4double* type

code show as below

#include<stdio.h>
int main()
{
	int a = 0x11223344;
	double* p = (double*)&a;
	*p = 0;
	return 0;
}

The debugging results are as follows

 It modifies 8 bytes;

1.5 Summary

After the above experiment we can get:

The int* type can modify 4 bytes;

The char* type can modify 1 byte

The short* type can modify 2 bytes;

double* can modify 8 bytes.

2. Pointer addition and subtraction of integers

2.1 Pointer plus integer

We use pointers and integers to modify the array. The code is as follows:

#include<stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 },i;
	int* p = arr;
	for (i = 0; i < 10; i++)
	{
		*p = 0;
		printf("%d ", *p);
		p++;
	}
	return 0;
}

Running the program can get

In the relationship between pointers and arrays, we can see it as the figure below. If you want to know more, you can check the link above.

 

 2.2 Pointer minus integer

Similarly, we modify the array, the code is as follows:

#include<stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }, i;
	int* p = &arr[9];
	for (i = 0; i < 10; i++)
	{
		*p = 0;
		printf("%d ", *p);
		p--;
	}
	return 0;
}

Running the program we can see

 2.3 Summary

        It seems that the two methods are the same, but the second method is indeed better for the following reasons: in the stack area, the high address is occupied first and the low address is occupied, so we can roughly draw

        If we choose the first method, it will be easy to modify our previous data once the limit is exceeded, resulting in data loss, but the second method will not cause this situation.

That’s it for today’s content, I hope you can connect three times with one click.

Guess you like

Origin blog.csdn.net/Infernal_Puppet/article/details/132025645