In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about the use of pointers in the C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. the function of the pointer
Pointers can be used to effectively express some complex data structures, such as systematic dynamic memory allocation, message mechanism, task scheduling, timers and so on. Mastering pointers can make your program more concise, compact, and efficient. So in the field of single-chip microcomputer, if you are doing a slightly larger project, you need to modularize each function, and the hardware driver layer and the application layer run independently, even if you change the single-chip microcomputer model, you don't have to modify the application layer program, that is, it is very portable. All of these are inseparable from pointers. Even without pointers, it can be difficult to implement, and even if you implement the code portability is very poor.
If the pointer is used incorrectly, it may cause a memory overflow error and cause the program to 'crash'.
Address and pointer
Pointer is a relatively abstract concept. If we really want to understand pointers, we should start with how the data is stored in memory. Let's look at how the data is stored in memory through a diagram.
The red box is the address of the memory, the green box is the data below the address, and the orange box is the offset of the memory. Summary: memory can be accessed by address.
Third, pointer variables
Variable type * variable name
Unsigned char * pstinct unsigned char a Ten pacifica
In this code, we define a variable an and a pointer variable p. We assign the memory address of the variable a to the variable p through the operator & so p points to the memory address of the variable a.
The problem of pointer variable assignment is mentioned above. So how to get and change the data that the pointer variable points to that memory address, we can pass:
* pointer variable = numeric value. For example, * p = 10
Do this to change the data of the memory address that the pointer variable points to.
Pass: a = * p
To get the data that the pointer variable points to that memory address.
Arrays and pointers
Generally, the system or compiler will allocate memory with contiguous addresses to store the elements in the array. If you assign the array address to the pointer variable, you can refer to the array and read and write the elements in the array through the pointer variable. The specific methods are as follows:
Pointer variable = & array name [subscript]
Or
Pointer variable = array name
For example: P = & buff [0]; or p = buff
P is the pointer variable and buff is the array. You can assign the array address to the pointer variable in both ways.
5. pointer self-addition and self-subtraction
Pointer variables can not only be used to obtain the value of the memory address, but also can be used for addition and subtraction. Well, this addition and subtraction is different from the addition and subtraction of ordinary variables. Ordinary variables add and subtract values, while pointer variables add and subtract addresses.
Two-dimensional values and pointers:
Two-dimensional arrays, like one-dimensional arrays, are assigned contiguous addresses to store data.
6. A pointer to a pointer
A pointer variable points to an integer variable or a character variable. Of course, it can also point to the storage address of the pointer variable, which can be referred to as a double pointer.
Definition method:
Data type * * pointer variable name
For example: unsigned char * * p
This means that a pointer variable p pointing to a pointer is defined, which points to another pointer variable.
7. Pointer variables as function parameters
Generally speaking, we take character type, integer type, array and so on as formal parameters of functions, in addition, pointer variables can also be used as formal parameters, and very often, the main purpose is to change the value of the pointer pointing to the address. the technical term is to change the value of the argument through the parameter.
8. Function pointer
If a function is defined in the program, the system allocates a storage space to the function code at compile time, and the first address of this storage space is called the address of the function. And the function name represents this address. Since it is an address, we can define a pointer variable to store it. This pointer variable is called a function pointer variable, or function pointer for short.
Definition of function pointer: function return value type (* pointer variable name) (function parameter list)
9. Array of function pointers
Like character type, shaping can be defined individually or as an array, and function pointers can also be defined as an array. The function pointer array is defined in the following format:
Function returns value type (* pointer variable name [array size]) (function argument list)
# include / * if you know the memory address, you can change the value of the variable by the memory address. * (unsigned int *) 0x404090 = 12 alternative Enum / * enumerate * / {led1, led2, led3, led_sum / * represents the total number of enumerated variables, used to flexibly define the array * /}; void drive_led1 (unsigned char sta) {if (sta) printf ("led1 on\ r\ n") Else printf ("led1 off\ r\ n");} void drive_led2 (unsigned char sta) {if (sta) printf ("led2 on\ r\ n"); else printf ("led2 off\ r\ n");} void drive_led3 (unsigned char sta) {if (sta) printf ("led3 on\ r\ n"); else printf ("led3 off\ r\ n") } void (* functional [sum]) (unsigned char sta) = {drive_led1,drive_led2,drive_led3}; / * function pointer array * / void xxx1 () {printf ("func1 running\ r\ n");} void xxx2 () {printf ("func2 running\ r\ n");} void xxx3 () {printf ("func3 running\ r\ n");} void (* func1 [3]) () = {xxx1,xxx2,xxx3} / * function pointer array * / unsigned char (* func) (unsigned char, unsigned char); / * function pointer * / unsigned char sum (unsigned char v1) unsigned char v2) {return v1 pointer v2;} void setvlue (unsigned char * p) {* p = 20;} int main () {unsigned char a; setvlue (& a); func=sum; a=func (1m 2); a = (* func) (1m 2); / * or a=func (1J 2) Can execute * / printf ("aversion% d\ r\ n", a); func1 [0] (); / * function pointer array * / func1 [1] (); / * function pointer array * / func1 [2] (); / * function pointer array * / funcled [led3] (1); / / Control lights, one code can return 0;} 10, pointer application scenarios
There are two main functions in the embedded field:
1. Do the underlying operating system, such as memory management, message queue and so on.
2. Do modular program interface.
Thank you for reading! This is the end of this article on "examples of the use of pointers in C language". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!
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.