Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the Application of C language pointer

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "C language pointer application case analysis". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "C language pointer application case analysis".

1. What is the pointer

Two main points of pointer understanding:

The pointer in spoken language usually refers to the pointer variable, which is used to store the memory address.

The pointer is the number, or address, of the smallest unit in memory

1.1 pointer variable

We can take out the actual memory address of the variable through & (take address operator) and store the address in a variable, which is the pointer variable:

Int main () {int a = 1 racket / opening up a block of space in memory / / a variable takes up 4 bytes of space, where the address of the first byte of an is stored in the p variable, p is a pointer variable int* pa = & a / / here we take out the address of the variable a, using the & operator printf ("% p\ n", & a); printf ("% p\ n", pa); a = 10; printf ("% p\ n", & a); return 0;}

Use memory and monitoring to see changes in & an and pa:

See the following figure:

& an is the address of the variable a: 0x00CFFEE0

Pa is a pointer variable, and the value stored is the address of the variable a: 0x00CFFEE0

The variable a takes up 4 bytes of space. In the following figure, you can see that the address of an occupies 4 bytes and stores 0 00 00 01

The number of columns displayed in memory can be adjusted according to yourself. 4 columns means 4 bytes in a row, as shown in the following figure:

If you reassign a to 10, the value of the variable a changes, but the address remains the same.

Therefore, the pointer variable, the variable used to store the address. Values stored in pointers are treated as addresses.

1.2 the pointer is the number of the smallest unit in memory

The pointer is the number of the smallest unit in memory, which is a byte

After careful calculation and balance by the experts, it is found that it is more appropriate to give a byte to a corresponding address.

For a 32-bit machine, assuming that there are 32 address lines, then assuming that each address line produces high (high voltage) and low (low voltage) levels (1 or 0) when addressing, the addresses generated by the 32 address lines are:

00000000 00000000 00000000

00000000 00000000 00000000 00000001

...

11111111 11111111 11111111

The minimum memory unit is one byte, and the corresponding address is shown in the following figure:

Therefore, a 32-bit machine has 2 to the 32-power address. Each address is identified as a byte, so we can give:

2 ^ 32 Byte = = 2 ^ 32 / 1024 KB = = 2 ^ 32 / 1024 MB = = 2 ^ 32 / 1024 GB = = 4 GB

4 GB's spare time for addressing. In the same way, the 64-bit machine, if given 64 address lines, can address 8 GB space.

On a 32-bit machine, the address is 32 zeros or ones in a binary sequence, so the address has to be stored in 4 bytes, so the size of a pointer variable should be 4 bytes.

On a 64-bit machine, there are 64 address lines, and the size of that pointer variable is 8 bytes to store an address.

The pointer is used to store the address, which uniquely indicates an address space.

The size of the pointer is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms

Int main () {int a = 10; int* pa = & a; char ch = 'aura; char* pc = & ch; printf ("% d\ n", sizeof (pa)); / / 4 printf ("% d\ n", sizeof (pc)); / / 4 return 0;}

The above example shows that no matter what type of pointer variable, its size is 4 bytes. Because the address is represented by 32-bit 0 1, and the pointer variable is the variable in which the address is stored, which requires 4 bytes. It has nothing to do with his type.

2. Pointers and pointer types

There are different types of variables, plastic, floating-point, etc. The pointer also has a type:

Int num = 10 * p = & the address of num;//num is saved to p

To save & num (the address of num) to p, we know that p is a pointer variable, so what is its type? We give the corresponding type of pointer variable

Char * pc = NULL

Int * pi = NULL

Short * ps = NULL

Long * pl = NULL

Float * pf = NULL

Double * pd = NULL

The pointer is defined as: type + *

The pointer of the char* type is to store the address of the char type variable: that is, the variable in the address is of type char, which occupies 1 byte, and the address itself is 4 bytes.

The pointer of the short* type is to store the address of the short type variable: that is, the variable in the address is of type short, which occupies 1 byte, and the address itself is 4 bytes.

The pointer of the int* type is to store the address of the int type variable: that is, the variable in the address is of type int, which occupies 4 bytes, and the address itself is 4 bytes.

2.1 pointer ±type int main () {int n = 10; char* pc = (char*) & n; int * pi = & n; printf ("% p\ n", & n); printf ("% p\ n", pc); printf ("% p\ n", pc+1); printf ("% p\ n", pi); printf ("% p\ n", pi+1) Return 0;}

The type of pointer determines how far the pointer takes a step forward or backward:

Int * backwards + 1, which moves to the address of the variable of the next int class, that is, moves 4 bytes

Char * backwards + 1, which moves to the address of the next variable of type char, that is, moves 1 byte

The type of pointer that points to the type of value stored in the address

2.2 dereferencing of pointers

The type of pointer determines how much permission you have when dereferencing the pointer (how many bytes can be manipulated)

2.2.1 dereferencing int main () {int a = 0x11223344; int* pa = & a; * pa = 0; return 0 for int* type

The dereferencing of the int* pointer can access four bytes:

2.2.2 dereferencing of char* type int main () {int a = 0x11223344; char* pa = (char*) & a bang bang an is int*, so cast here * pa = 0; return 0;}

The pointer dereferencing of char* can only access one byte:

3. Wild pointer

A wild pointer is the position where the pointer points to is unknown (random, incorrect, undefined).

3.1 Wild pointer cause 3.1.1 the pointer is not initialized int main () {int * p / local variable pointer is not initialized, default is random value * p = 20; return 0;} 3.1.2 pointer out of bounds access int main () {int arr [10] = {0}; int * p = arr; int iTuno; for (iTun0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report