当前位置:网站首页>The way to access global variables in multi-source file mode (extern usage)
The way to access global variables in multi-source file mode (extern usage)
2022-07-24 00:43:00 【Forgotten dreams】
Browse
One . Example of the problem of global variables in multi file mode
Example 
When I want to pass a variable not only in the source file 1 To visit , And I'm still accessing source file 2 , I want to define a header file , Then the source file 1 Contains the header file , Source file 2 This header file is also included , Then define a variable in the header file .
I want to access this variable through File Inclusion , Get the overall effect .
but , The facts are not what people want , There was a mistake .
Two . Use global variables in multi file mode extern Way
To solve such problems , utilize extern How to solve , Multi file global variable problem .
extern, Declare a variable , And then define variables , This variable defines a global scope .
I declare variables in the header file a, utilize extern Statement a, Then in the main function a Define as global variable , At this time a It can be accessed in other files , At this time a No matter in which file , In the case of multiple source files , Other source files to access this a, I just need to include the header file , You can successfully access a
Output results 11
3、 ... and . The convenience benefits obtained – Fewer functions pass more parameters ( Creation and display of linked list )
main.cpp
#include <iostream>
#include " header .h"
using namespace std;
PNode head = (PNode)malloc(sizeof(Node));
int main()
{
head->next = NULL;
Create_Node();
Display();
return 0;
}
Function module
#include " header .h"
#include <iostream>
using namespace std;
void Create_Node()
{
int i;
PNode p = head;
for (i = 0; i < 3; i++)
{
PNode q = (PNode)malloc(sizeof(Node));
q->Node = i;
q->next = p->next;
p->next = q;
}
}
void Display()
{
PNode p = head->next;
while (p != NULL)
{
cout << p->Node << " ";
p = p->next;
}
}
The header file
#pragma once
typedef struct Node
{
int Node;
struct Node* next;
}Node, *PNode;
extern PNode head;
void Create_Node();
void Display();
Output results
2 1 0
utilize extern Set the pointer to the head node of the linked list to global , The advantage of this is , When the insertion module of the linked list node is in other source files , If I execute the function of inserting new nodes , Then I have to find the pointer to the header node of the table , Then we can find the pointer of the header and insert , Because the pointer of the head node is global , So I can use it directly through the source file of the function module , There is no need to use the parameters of the function to find the head node of the table , It mainly reduces the number of parameters passed by the function .
About extern Specific other usage , Reference resources :https://www.cnblogs.com/mch0dm1n/p/5727667.html
边栏推荐
- GBase 8c 位串操作符(一)
- First knowledge of C language functions
- 网络系统实验:ping不通的问题解决
- postman测试接口在URL配置正确的情况下出现404或者500错误
- Gbase 8C access authority query function (II)
- 【LeetCode第 83 场双周赛】
- PayPal subscription process and API request
- Bean Validation自定义容器验证篇----06
- High number_ Chapter 1 space analytic geometry and vector algebra__ Two point distance
- Intelligent OCR identification of express documents helps the logistics industry to upgrade Digitalization
猜你喜欢

Bean validation usage article ----05

A good habit to develop when writing SQL

泛型机制和增强for循环

【电赛训练】非接触物体尺寸形态测量 2020年电赛G题

How to speed up matrix multiplication -- optimizing GEMM (CPU single thread)

MySQL client to server character set conversion

Redis cluster hash sharding algorithm (slot location algorithm)

Treatment of particle boundary collision

SAP 电商云 Spartacus UI Store 相关的设计明细

English grammar_ Demonstrative pronoun -such / the same
随机推荐
Beifeng communication appeared in China (Xiamen) emergency exhibition | intelligent communication means are strong and eye-catching!
Gbase 8C session information function (III)
C language: deep analysis of const keyword
The high-quality digital collection of guochuang's "children's song line" is on sale, and you are invited to create a young martial arts Jianghu dream
Intelligent OCR identification of express documents helps the logistics industry to upgrade Digitalization
High number_ Chapter 2 differential calculus of multivariate functions__ Geometric application of partial derivatives_ Tangent and normal plane of space curve
Gbase 8C access authority query function (II)
win10下基于anaconda的detectron2安装
SAP 电商云 Spartacus UI Store 相关的设计明细
暑假第四周总结
数仓数据标准详解-2022
Flutter | the easiest way to add header and footer to listview
MySQL table field quantity limit and row size limit
How to speed up matrix multiplication -- optimizing GEMM (CPU single thread)
CA digital certificate
Overview of data model design method
Codeforces Round #807 (Div. 2)(A-D)
Educational Codeforces Round 132 (Rated for Div. 2) D. Rorororobot
A good habit to develop when writing SQL
Accelerating matrix vector multiplication of special matrices with FFT