外部文件调用结构体变量
2010-10-13 22:16
281 查看
//file: head.h
struct test
{ int i;
char j;
}; // 这里不能定义任何变量,因为头文件被许多文件包含,会出现重复定义
extern struct test *right; // 申明结构体变量 right 在其它文件中定义
/*******************************************************/
//file: use.c
struct test *right; // 在这里将 right 定义为全局变量
//file: other.c
#include "head.h" // 只要将头文件包含进去,任何文件都可以调用 right;
void over()
{ printf("%c",right->j); // 直接调用结构体变量 right 的成员
}
感谢大家的参与!
楼下有些朋友提到了这种观点:一个变量可以多次申明,不管是否在一个文件中,申明和定义可以同时出现。我做了错误的反驳,特此说明。
例子:
test.h:
typedef struct
{
int a;
char b[12];
} test;
extern test *t;
***********************************************************************
test.c:
#include ;
#include ;
#include "test.h"
test *t;
int main()
{
t = (test *)malloc(sizeof(test));
t->;a = 2;
strcpy(t->;b,"abc");
test_func();
free(t);
return 0;
}
************************************************************************
test_func.c:
#include ;
#include "test.h"
void test_func()
{
printf("t->;b = %s/n",t->;b);
}
struct test
{ int i;
char j;
}; // 这里不能定义任何变量,因为头文件被许多文件包含,会出现重复定义
extern struct test *right; // 申明结构体变量 right 在其它文件中定义
/*******************************************************/
//file: use.c
struct test *right; // 在这里将 right 定义为全局变量
//file: other.c
#include "head.h" // 只要将头文件包含进去,任何文件都可以调用 right;
void over()
{ printf("%c",right->j); // 直接调用结构体变量 right 的成员
}
感谢大家的参与!
楼下有些朋友提到了这种观点:一个变量可以多次申明,不管是否在一个文件中,申明和定义可以同时出现。我做了错误的反驳,特此说明。
例子:
test.h:
typedef struct
{
int a;
char b[12];
} test;
extern test *t;
***********************************************************************
test.c:
#include ;
#include ;
#include "test.h"
test *t;
int main()
{
t = (test *)malloc(sizeof(test));
t->;a = 2;
strcpy(t->;b,"abc");
test_func();
free(t);
return 0;
}
************************************************************************
test_func.c:
#include ;
#include "test.h"
void test_func()
{
printf("t->;b = %s/n",t->;b);
}
相关文章推荐
- linux下ftp命令调用外部变量传送文件
- C语言外部调用结构体,变量和数组
- 在Matlab中调用外部的exe文件,并将Workspace 中的变量作为参数传递给exe文件
- html调用外部css文件和js文件
- 多个文件共享结构体变量
- 结构体变量作函数参数实现传值调用。
- VC6.0下调用Dll文件提供的函数接口和全局变量(转)
- Silverlight调用WebService 之 Silverlight动态加载外部XML指定地址的WebService---(动态加载外部XML文件中指定的WebService地址)
- 利用python调用外部的dll或者so文件,碉堡了
- applicationContext.xml文件如何调用外部properties等配置文件
- applicationContext.xml文件如何调用外部properties等配置文件
- 【原创_解析】关于static型变量被外部文件修改的问题..
- ROS编程中如何配置CMakelists.txt来调用外部的动态链接库(.so文件)
- java spring调用db.properties外部文件时出错
- 每日总结:每个 GROUP BY 表达式必须至少包含一个不是外部引用的列、加载页面时调用Js方法、调用Js文件中的方法
- 解决ExecuteFile调用外部的VBS文件时提示”无效字符”的问题
- c写个linux的小程序,从bin文件中读取数据,打印结构体中的成员变量。
- go程序中可以被外部包调用的函数和变量
- How to:如何在调用外部文件时调试文件路径(常见于使用LaunchAppAndWait和LaunchApp函数)
- 用接口实现通过改外部文件实现判断调用接口内的方法