In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the use of pointers in C language, the article is very detailed, has a certain reference value, interested friends must read it!
(1) dare to ask what the pointer is 1.1 the concept of pointer
In computer science, a Pointer is an object in a programming language whose value points directly to (points to) a value stored in another place in the computer memory using an address. Since the desired variable unit can be found through the address, it can be said that the address points to the variable unit. Therefore, the address is visualized as a "pointer". It means that the memory unit with its address can be found through it.
Then we can understand it this way-"the pointer is a variable that holds the address (number) of the memory unit."
Summary: pointers are variables, variables used to store addresses. Values stored in pointers are treated as addresses.
1.2 behind the pointer
After careful calculation and balance, we find 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, assume that each address line is addressing to generate an electrical signal positive / negative (1 or 0)
There are addresses of 2 to the power of 32.
Here we understand:
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 of space, so the size of a pointer variable should be 4 bytes.
On a 64-bit machine, if there are 64 address lines, the size of that pointer variable is 8 bytes to store an address.
The pointer is used to store the address, which uniquely identifies an address space.
The size of the pointer is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms.
(II) pointer and pointer class 2.1 types of pointers
Here we are talking about: we all know the type of pointer, there are different types of variables, shaping, floating-point type, and so on. Is there a type of pointer? To be exact: yes.
When there is code like this:
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 pointer variable the corresponding type.
As you can see here, pointers are defined as: type + *. Actually: the pointer of the char* type is to hold the address of the variable of the char type. The pointer of the short* type is to hold the address of the variable of the short type. The pointer of the int* type is to hold the address of the variable of the int type.
2.2 the meaning of pointer types
As an example in the following figure: using char*, you can store addresses of type int (all four bytes), but is it really that easy?
Let's compare a set of operations:
We can see that the assigned an only changes the size of one byte.
To sum up, we can draw the following conclusions:
The pointer type determines how many bytes are accessed at a time (that is, the size of the access memory) when the pointer dereferencing operation.
The type of pointer determines how much (distance) the pointer takes forward or backward.
Determines the step size of the pointer +-integer
If you are interested, you can also analyze the following code to deepen your understanding of pointer types.
2.3 dereferencing of pointers
The implementation of this code is left to everyone to debug ✅✅✅.
Summary: the type of pointer determines how much permission you have when dereferencing the pointer (a few bytes can be manipulated). For example, the pointer dereference of char* can only access one byte, while the dereference of int* pointer can access four bytes.
The above is all the content of the article "what is the use of pointers in C language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.