C 中的结构问题,使用相同的结构多个源文件
Struct Issue in C, using same Struct Multiple Source Files
我有一个 'struct',我想在多个源文件中使用它。我已经在 Header 文件中声明了结构,然后包含在源文件中。如果有人可以帮助我解决这个问题,那就太好了。
我正在发布 Header、来源和错误
#ifndef DATABASE_H
#define DATABASE_H
struct dataBase
{
char modelName;
float capacity;
int mileage;
char color;
};
extern struct dataBase Inputs;
#endif /* DATABASE_H */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "dataBase.h"
struct dataBase Inputs = NULL;
//size_t Inputs_Size = 0;
int main (void)
#include "hw4_asharma_display.h"
#include <stdio.h>
#include "dataBase.h"
void printLow(int size)
{
// Declaring Variables
int i;
for(i=0; i<size; i++)
{
printf("%s %f %d %s\n",
Inputs[i].modelName,
Inputs[i].capacity,
Inputs[i].mileage,
Inputs[i].color);
}
hw4_asharma_display.c:14:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
~~~~~~^~
hw4_asharma_display.c:29:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
Inputs
不是数组,所以不能只使用 [i]
索引符号。您必须更改其声明:
struct dataBase Inputs = NULL;
(顺便说一句,NULL
部分毫无意义)到
struct dataBase Inputs[N];
相反,如果您打算只有一个元素,请保留声明:
struct dataBase Inputs;
但删除 [i]
部分:
printf("%c %f %d %c\n",
Inputs.modelName,
Inputs.capacity,
Inputs.mileage,
Inputs.color);
此外,您必须在打印前填写每个元素,否则您将得到所有零和空白。
我有一个 'struct',我想在多个源文件中使用它。我已经在 Header 文件中声明了结构,然后包含在源文件中。如果有人可以帮助我解决这个问题,那就太好了。 我正在发布 Header、来源和错误
#ifndef DATABASE_H
#define DATABASE_H
struct dataBase
{
char modelName;
float capacity;
int mileage;
char color;
};
extern struct dataBase Inputs;
#endif /* DATABASE_H */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "dataBase.h"
struct dataBase Inputs = NULL;
//size_t Inputs_Size = 0;
int main (void)
#include "hw4_asharma_display.h"
#include <stdio.h>
#include "dataBase.h"
void printLow(int size)
{
// Declaring Variables
int i;
for(i=0; i<size; i++)
{
printf("%s %f %d %s\n",
Inputs[i].modelName,
Inputs[i].capacity,
Inputs[i].mileage,
Inputs[i].color);
}
hw4_asharma_display.c:14:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
~~~~~~^~
hw4_asharma_display.c:29:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
Inputs
不是数组,所以不能只使用 [i]
索引符号。您必须更改其声明:
struct dataBase Inputs = NULL;
(顺便说一句,NULL
部分毫无意义)到
struct dataBase Inputs[N];
相反,如果您打算只有一个元素,请保留声明:
struct dataBase Inputs;
但删除 [i]
部分:
printf("%c %f %d %c\n",
Inputs.modelName,
Inputs.capacity,
Inputs.mileage,
Inputs.color);
此外,您必须在打印前填写每个元素,否则您将得到所有零和空白。