On the High-order Usage of Void Pointer

To flexibly use C language to implement some high-level frameworks, you need to master some advanced programming skills. This article will talk about some wonderful uses of void pointer. The test environment adopts IAR for arm 8.40.1

What is a void pointer? A void pointer is generally called a universal pointer or a universal pointer. It is a convention of pure address in C language. When a pointer is a void pointer, the object pointed to does not belong to any type. Because the void pointer does not belong to any type, it cannot be arithmetically operated, such as autoincrement. The compiler does not know how much it needs to be increased. For example, for a char * pointer, the self increment must be the address pointed to by the pointer plus 1, and for a short * pointer, the offset is 2.

In C / C , other types of pointers can be used to replace void pointers at any time, or void pointers can be used to replace other types of pointers.

Many useful techniques can be derived from these features. The essence of a pointer is that its value is an address, so extend it:

When a pointer variable is declared with the keyword void, it becomes a generic pointer variable. The address of any variable of any data type (char, int, float, etc.) can be assigned to the void pointer variable.

For dereference of pointer variables, use the indirect operator * to achieve the purpose. However, when null pointers are used, pointer variables need to be converted to dereference. This is because a null pointer has no data type associated with it. The compiler cannot know the data type pointed to by the void pointer. Therefore, to obtain the data pointed by the void pointer, you need to use the correct type of data stored in the void pointer position for type conversion.

For the dereference of null pointers, if you don't believe it, take a look at chestnuts:

You see, direct dereference compilation, but because the compiler is masked.

However, it should be noted that:

Different compilers deal with void pointers differently. For example, IAR, ANSI C and VC will make errors for the above, and the algorithm operation of GNU specifying "void" is consistent with "char". Therefore, the above writing method can be compiled in GNU

Therefore, make a type conversion and correct it as follows:

Void pointer dereference must be specified.

Pay attention to type matching during type conversion.

In addition, if the function type can be any type of pointer, its parameters need to be defined as void * pointer, such as the function set about memory operation in string. H:

__ EFF_ NENW1NW2 __ ATTRIBUTES int memcmp(const void *, const void *,


__ EFF_ NENR1NW2R1 __ DEPREC_ ATTRS void * memcpy(void *_Restrict,

const void *_Restrict,


__ EFF_ NENR1NW2R1 __ DEPREC_ ATTRS void * memmove(void *, const void *,


__ EFF_ NENR1R1 __ DEPREC_ ATTRS void * memset(void *, int, size_t);

The application of nonvolatile storage management in the development of single chip microcomputer often needs to realize the nonvolatile storage of data. The so-called nonvolatile storage means that the data can be maintained after power failure after rewriting. What are nonvolatile storage media? For example, EEPROM and flash belong to nonvolatile storage media.

For example, there are many and various parameters in a product, which are distributed in each subsystem file. For example, chestnuts:

/*There is such a structure in module a that requires nonvolatile storage * / typedef struct_ t_ paras

Int language; / * language type*/

Char Sn [20]; / * product serial number*/


T_ PARAS sysParas;

/*There is a structure in module B that requires nonvolatile storage * / typedef struct_ t_ pid

float kp;

float ki;

float kd;

float T;


T_ PID pidParas;

Facing such a demand, to realize nonvolatile storage, I store and manage the above application data in a certain order based on the implementation of the underlying EEPROM / flash read-write function. So what is the more ideal way? A module is designed to store nonvolatile data. For example:

typedef struct _ t_ nv_ layout

Void * pelement; / * parameter address*/

Int length; / * parameter length*/


/*Parameter mapping table*/

T_ NV_ LAYOUT nvLayout[]=

& sysparas, sizeof (t_paras), / * parameter mapping record*/




/*Number of records in parameter mapping table * / #define NV_ RECORD_ NUMBER (sizeof(nvLayout)/sizeof(T_NV_LAYOUT))void nv_ load(T_NV_LAYOUT *pLayout,int nvAddr,int number);

void nv_ store(T_NV_LAYOUT *pLayout,int nvAddr,int number);

Describe the above design ideas with UML:

On the above basis, we only need to design the hardware layer abstraction to design a feasible and general NV management subsystem. In this way, the designed subsystem ignores the business data, only processes it as data, and does not care about its business significance. It realizes the isolation and decoupling between business logic and background. Universal. Here, the characteristics of void * pointer are cleverly used. If the design idea is further extended, the underlying abstraction is encapsulated in a layer, and the more detailed underlying implementation details are isolated from the abstraction, such as:

Abstract I2C / SPI EEPROM and unify its calling interfaces to the upper layer. If your system was originally stored in I2C EEPROM and now you need to use another SPI interface EEPROM for a new project, you only need to implement the corresponding underlying processing function.

Abstract storage media, such as EEPROM / data flash, etc...


So how to do the underlying abstraction? We can use the function pointer to define a unified interface. During specific deployment, we only need to assign the pointer of the implementation function to the corresponding function pointer, so as to achieve the abstract unification of the interface. In fact, this is a simple prototype of the driving model.

To sum up, this article introduces some programming ideas, which are useful for MCU / embedded advanced programming:

Using the void * pointer, the business data is abstractly decoupled from the underlying storage

Using layered abstraction, the code has good portability

The idea of virtual function definition interface in high-level languages such as C is realized by using function pointer

The bottom layer of the unified interface implements abstraction and realizes the idea of driving layering

Void * pointer can be extended to many similar applications from this example

Enlightenment: if we deeply understand the mechanism behind some language details, we can get many ingenious applications.

Disclaimer: the material of this article comes from the Internet, and the copyright belongs to the original author. If the copyright of the work is involved, please contact me to delete it.

Editing: JQ

On the High-order Usage of Void Pointer 1

get in touch with us
recommended articles
Han's Laser Made a Profit of 124 Million by Selling 50% Equity of Han's Robot
Recently, Han's laser pointed out in its 2020 performance express that during the reporting period, the company sold part of the equity of Han's robot and confirmed an investment income of about 124 million yuan.According to the announcement, the annual revenue of Han's laser in 2020 is 12.032 billion yuan, a year-on-year increase of 25.83%; The annual total profit was 690 million yuan, a year-on-year increase of 46.5%; The contribution of the proceeds from the sale of Han's robot to the total profit reached 17.97%.On September 18, 2020, Han laser officially announced the sale of Han robot. Han laser said that in order to stabilize the core management team of the company's robot business and stimulate its R & D creativity, Han laser signed the equity transfer agreement with Shenzhen zhirentuan enterprise management partnership, Wang Guangneng and Zhang Guoping, which agreed to transfer 50% equity of Han robot held by the company to zhirentuan at the price of 80 million yuan.Wang Guangneng, general manager of Han's robot, told "senior robot" that in order for the rapidly developing Han's robot to obtain more development resources, including the injection of high-quality capital, the improvement of the industrial chain and even the listing in the future, Han's robot needs to move from behind the scenes to in front of the group.In the second month after Han laser announced the asset sale, Han robot announced that it had completed a round of financing of RMB 165 million. This round of funds will be mainly used to accelerate the R & D and market promotion of Han intelligent cooperative robot and promote its application in industry, medical treatment, logistics, services and other fields.Then, on November 9, 2020, Han's robot won a plot in the core area of sanlongwan, Foshan for 214.37 million yuan, which will be used to build a centralized demonstration park for advanced manufacturing of Han's robot. The project covers an area of 225.51 mu, with a planned total construction area of about 600000 square meters and a planned total investment of 2 billion yuan. The global robot headquarters, robot R & D and incubation base and advanced manufacturing centralized demonstration area will be built in Shunde area of Sanlong Bay.In the future, the Han robot advanced manufacturing centralized demonstration park project will attract more than 20 upstream and downstream enterprises from Shenzhen to gather and develop in the area, including 15 high-tech enterprises and 10 enterprises with sales of more than 200 million yuan, forming industrial advantages and providing strong support for the development of the robot industry in the future.At the same time, collaborative robot products, which are the product basis for realizing the ambition of Han's robot, are also undergoing rapid iteration and upgrading.It is reported that the Han robot elfin series cooperative robots include four load levels of 3kg, 3.5kg, 5kg, 8kg and 10kg. They are designed with two joint modules. One motion module contains two joints, forming a unique kinematic structure. They are not only different from most cooperative robots on the market in appearance, but also obtain higher flexibility when working.In December 2020, Han's robot officially released the upgraded collaborative robot elf-p. In addition to all the advantages of elfin series cooperative robots, the load capacity of elfin-p series cooperative robots is further improved. The maximum load can reach 4kg, 6kg, 8kg and 12kg respectively, and the arm span is 590mm, 800mm, 1300mm and 1000mm respectively. In addition, an extended arm span robot is added to 4kg load, and the working range can reach 950mm. As a whole, the application scope of elfin series cooperative robots is further expanded.In addition, on November 13, 2020, the new generation of intelligent robot, which is the crystallization of the wisdom of the Chinese and German technical teams, will debut in the form of an online conference and will be officially released to domestic users at the Han's robot innovation product Conference on March 26, 2021.Wang Guangneng summarized the characteristics of the new generation of intelligent robots as "poss": "P" stands for performance, "O" stands for open, and the two "s" stand for safety and smart respectively.Specifically, Han's new generation of intelligent robot adopts modular design, and each axis realizes multi-sensor fusion of vision, touch, force and so on. Combined, a robot is equipped with hundreds of sensors. Users can teach the robot in many ways, such as code, drag, gesture, voice and so on.The intelligent robot also has excellent performance in hardware performance indicators such as accuracy, speed, arm spread, load and degree of freedom. For example, the repeated positioning accuracy can reach ± 0.01mm, the speed of end tools is 3.5m/s, and the end is compatible with a variety of end actuators and even pneumatic fixtures, which can meet most communication modes on the market.At the same time, the robot adopts a dual redundant architecture, including sensors, encoders, gas circuits, etc., and is equipped with electronic skin and independent safety controller to further ensure the safety of the robot.At present, this new generation of intelligent robot has entered the stage of small batch trial production, and has obtained intentional orders from many international well-known large enterprises.In the highly competitive cooperative robot market, Wang Guangneng said that there are hundreds of different forms of cooperative robots on the market. There is no doubt that cooperative robots must be the future. However, it is inevitable that some manufacturers will be eliminated during this period, and the development and progress of the industry will be synchronized with the reshuffle. In this process, whoever can master the core key technologies and continuously develop products that meet the needs of the scene can lead the industry and obtain competitive advantages.Han robot is making great strides towards this goal.Original title: [Hechuan shares | Focus] sold 50% equity of Han's robot, and Han's laser made a profit of 124 millionThe source of the article: WeChat official account: Gao Gong robot] welcome to add attention! Please indicate the source of the article.
Main Characteristics of Laser
What Are Some Ways to Keep My Hair Really Healthy?
How Was the First Sale of Vivo X60 Series?
Huami Technology Held a Press Conference on August 27, and Amazfit Smart Sports Watch 3 Officially A
Connect to Nonencrypted Wireless Network Using Ubuntu Commands
Is Time Travel Possible? Can We Travel Back in Time?
Which Is a Better Design to Put on a Beer Pong Table?
What Else to Do on PS3?
How to Protect Data on IOT Devices
related searches
Han's Laser Made a Profit of 124 Million by Selling 50% Equity of Han's Robot
Main Characteristics of Laser
What Are Some Ways to Keep My Hair Really Healthy?
How Was the First Sale of Vivo X60 Series?
Huami Technology Held a Press Conference on August 27, and Amazfit Smart Sports Watch 3 Officially A
Connect to Nonencrypted Wireless Network Using Ubuntu Commands
Is Time Travel Possible? Can We Travel Back in Time?
Which Is a Better Design to Put on a Beer Pong Table?
What Else to Do on PS3?

Copyright © 2020 Coffee bag - Guangzhou tianci packaging industry Co,. Ltd. | Sitemap