Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C全局结构指针_C_Variables_Pointers_Struct_Global Variables - Fatal编程技术网

C全局结构指针

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

我在文件中声明了一个类型定义的结构。我有一个指向它的指针,希望在多个文件中将其作为全局变量使用。有人能指出我做错了什么吗

fileA.h:

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!