当前位置:网站首页>多源文件方式去访问全局变量的方式(extern用法)
多源文件方式去访问全局变量的方式(extern用法)
2022-07-24 00:35:00 【被遗忘的梦想】
一.多文件方式全局变量所在问题例子
示例
当我想通过一个变量不但能够在源文件1去访问,而且还在源文件二去访问,我想通过定义一个头文件,然后源文件1包含该头文件,源文件2也包含该头文件,然后再该头文件里面定义一个变量。
我想通过文件包含的方式去访问该变量,获得全局的效果。
但,事实不尽人愿,出了一个错误。
二.多文件方式使用全局变量extern办法
为了解决这样的问题,利用extern的方式解决,多文件全局变量问题。
extern,声明一个变量,然后定义变量,该变量定义的作用域是一个全局才行。
我在头文件声明变量a,利用extern声明a,然后在主函数吧a定义成全局变量,此时的a就可以在其他文件里面被访问了,此时的a不管在哪一个文件,多个源文件情况下,其他源文件去访问这个a,我只需要包含头文件就行,就能够成功访问到a
输出结果 11
三.获得的便利好处–减少函数传递更多的参数(链表的创建与显示)
main.cpp
#include <iostream>
#include "标头.h"
using namespace std;
PNode head = (PNode)malloc(sizeof(Node));
int main()
{
head->next = NULL;
Create_Node();
Display();
return 0;
}
功能模块
#include "标头.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;
}
}
头文件
#pragma once
typedef struct Node
{
int Node;
struct Node* next;
}Node, *PNode;
extern PNode head;
void Create_Node();
void Display();
输出结果
2 1 0
利用extern将指向链表的头结点的指针设置全局,这样的好处是,当链表结点的插入模块在其他源文件时候,我如果执行插入新的结点功能,那么我必须得找到该表的指向头节点的指针,然后才能找到该表头的指针才能做插入的操作,由于头节点的指针是全局,所以我就直接通过功能模块的源文件直接用就行,不需要利用函数的参数进行找到该表的头结点,主要还是减少了函数传参数量的个数减少了。
关于extern具体其他用法,参考:https://www.cnblogs.com/mch0dm1n/p/5727667.html
边栏推荐
- Gbase 8C mode visibility query function (2)
- Classic examples of C language switch case statement conversion date format
- GBase 8c 会话信息函数(二)
- Tencent cloud was affirmed by international professional streaming media evaluation: video coding performance is the best in all three scenarios
- JS drag and drop element
- Detectron2 installation based on Anaconda under win10
- Gbase 8C access authority access function (IV)
- Difference between data index and label system of data warehouse
- Redis cluster hash sharding algorithm (slot location algorithm)
- There are various signs that apple is expected to support AV1
猜你喜欢

Classic example of C language - loan balance

Intelligent OCR identification of express documents helps the logistics industry to upgrade Digitalization

Network system experiment: solve the problem of Ping failure

Blockbuster | certik: Web3.0 industry safety report release in the second quarter of 2022 (PDF download link attached)

Delete all data of specified query criteria in Oracle

Codeforces Round #807 (Div. 2)(A-D)

paypal订阅流程及api请求

The prediction of domestic AI protein structure reproduced a breakthrough and solved the 3D structure with a single sequence. Peng Jian's team: "the last piece of puzzle since alphafold2 has been comp

数仓数据标准详解-2022

Redis 主从、哨兵、集群架构有缺点比较
随机推荐
PayPal subscription process and API request
English grammar_ Demonstrative pronoun - so
GBase 8c系统表信息函数(二)
Redis cluster hash sharding algorithm (slot location algorithm)
GBase 8c 访问权限查询函数(二)
Printf function - conversion description
JS determines whether the element scrolls to the top
泛型机制和增强for循环
Flutter | the easiest way to add header and footer to listview
《天幕红尘》笔记与思考(五)强势文化与弱势文化
[low code] limitations of low code development
The implementation in Oracle replaces the specified content of the specified column with the desired content
GBase 8c 访问权限查询函数(五)
Redis持久化机制RDB、AOF
How to realize 485 wireless communication between multiple sensors and Siemens PLC?
Method of C language annotation
GBase 8c 模式可见性查询函数(一)
GBase 8c 会话信息函数(五)
Bean Validation使用篇----05
数仓数据标准详解-2022