C全局结构指针
我在文件中声明了一个类型定义的结构。我有一个指向它的指针,希望在多个文件中将其作为全局变量使用。有人能指出我做错了什么吗 fileA.h:C全局结构指针,c,variables,pointers,struct,global-variables,C,Variables,Pointers,Struct,Global Variables,我在文件中声明了一个类型定义的结构。我有一个指向它的指针,希望在多个文件中将其作为全局变量使用。有人能指出我做错了什么吗 fileA.h: typedef struct { bool connected; char name[20]; }vehicle; extern vehicle *myVehicle; #include "fileA.h" void myFunction(){ myVehicle = m
typedef struct
{
bool connected;
char name[20];
}vehicle;
extern vehicle *myVehicle;
#include "fileA.h"
void myFunction(){
myVehicle = malloc(sizeof(vehicle));
myVehicle->connected = FALSE;
}
#include "fileA.h"
void anotherFunction(){
strcpy(myVehicle->name, "this is my car");
}
fileA.c:
typedef struct
{
bool connected;
char name[20];
}vehicle;
extern vehicle *myVehicle;
#include "fileA.h"
void myFunction(){
myVehicle = malloc(sizeof(vehicle));
myVehicle->connected = FALSE;
}
#include "fileA.h"
void anotherFunction(){
strcpy(myVehicle->name, "this is my car");
}
fileB.c:
typedef struct
{
bool connected;
char name[20];
}vehicle;
extern vehicle *myVehicle;
#include "fileA.h"
void myFunction(){
myVehicle = malloc(sizeof(vehicle));
myVehicle->connected = FALSE;
}
#include "fileA.h"
void anotherFunction(){
strcpy(myVehicle->name, "this is my car");
}
我得到的错误是:
文件A中提到的未定义的外部“myVehicle”
这是一项声明:
添加一个定义:
只有一个
.c
文件。有效。我可以发誓我以前试过,结果堆栈溢出了。但也许我搞错了。谢谢hmjd!