当前位置:网站首页>多源文件方式去访问全局变量的方式(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
边栏推荐
- Printf function - conversion description
- GBase 8c 访问权限访问函数(四)
- Simple implementation and analysis of binary search tree
- GBase 8c 会话信息函数(六)
- Accelerating matrix vector multiplication of special matrices with FFT
- 数据模型设计方法概述
- Don't let Fujin Yibo see this
- English语法_指示代词 - So
- Classic example of C language - loan balance
- php实现 Stripe订阅
猜你喜欢

Coloring old photos - deoldify get started quickly

Simple implementation and analysis of binary search tree

Redis master-slave synchronization mechanism

MySQL client to server character set conversion

Implementation of singleton mode in C #

Docker pulls the redis image and runs it

Problem note - unable to open include file: "direct.h": no such file or directory

Beifeng communication appeared in China (Xiamen) emergency exhibition | intelligent communication means are strong and eye-catching!

Expérience du système réseau: résoudre les problèmes de ping

Mobile terminal H5 - a lifeline timeline
随机推荐
PayPal subscription process and API request
How to speed up matrix multiplication -- optimizing GEMM (CPU single thread)
C language writing specification
Delete all data of specified query criteria in Oracle
win10下基于anaconda的detectron2安装
JS drag and drop element
Redis 主从、哨兵、集群架构有缺点比较
Blockbuster | certik: Web3.0 industry safety report release in the second quarter of 2022 (PDF download link attached)
Communication module sorting (II) hc-05
Summary of polynomial commitment schemes
ES6 combines multiple class methods
Redis data structure
GBase 8c 会话信息函数(一)
Redis persistence mechanism RDB, AOF
paypal订阅流程及api请求
Notes and Thoughts on the red dust of the sky (VI) due to lack
泛型机制和增强for循环
Gbase 8C session information function (4)
Semaphore
C language book recommendation