C 在单独的头文件中定义的结构
LMlib.h LMlib.cC 在单独的头文件中定义的结构,c,header,struct,C,Header,Struct,LMlib.h LMlib.c #ifndef LMlib_H #define LMlib_H #endif #define MAX_IP_LENGTH 15 #define MAX_TABLE_ROWS 255 struct ForwardingTableRow { char address[MAX_IP_LENGTH]; int subnetMask; int interface; }; typedef struct F
#ifndef LMlib_H
#define LMlib_H
#endif
#define MAX_IP_LENGTH 15
#define MAX_TABLE_ROWS 255
struct ForwardingTableRow
{
char address[MAX_IP_LENGTH];
int subnetMask;
int interface;
};
typedef struct ForwardingTableRow ForwardingTableRow;
我做错了什么 您有三个问题:第一个是没有正确使用数组索引。数组是
表
变量,而不是结构成员:
LMlib.c: In function ‘ReadForwardingTable’:
LMlib.c:11:27: error: request for member ‘address’ in something not a structure or union
LMlib.c:11:45: error: request for member ‘subnetMask’ in something not a structure or union
LMlib.c:11:66: error: request for member ‘interface’ in something not a structure or union
第二个问题与您的问题无关,但将来可能会导致麻烦。这是你的护卫。#endif
应该在文件的末尾,否则您只保护单个#define
而不保护其他内容
第三个也是最严重的问题是,address
字段中的字符太少。IP地址的最大长度是15,这是正确的,但如果要将其视为字符串,还需要为字符串终止符留出空间。宣布为
fscanf(f, "%s %d %d",
table[i].address,
&table[i].subnetMask,
&table[i].interface);
这应该没问题。您有三个问题:第一个是没有正确使用数组索引。数组是
表
变量,而不是结构成员:
LMlib.c: In function ‘ReadForwardingTable’:
LMlib.c:11:27: error: request for member ‘address’ in something not a structure or union
LMlib.c:11:45: error: request for member ‘subnetMask’ in something not a structure or union
LMlib.c:11:66: error: request for member ‘interface’ in something not a structure or union
第二个问题与您的问题无关,但将来可能会导致麻烦。这是你的护卫。#endif
应该在文件的末尾,否则您只保护单个#define
而不保护其他内容
第三个也是最严重的问题是,address
字段中的字符太少。IP地址的最大长度是15,这是正确的,但如果要将其视为字符串,还需要为字符串终止符留出空间。宣布为
fscanf(f, "%s %d %d",
table[i].address,
&table[i].subnetMask,
&table[i].interface);
一切都会好起来的。一切都是关于你的
的优先级高于&
,因此它基本上表示:
address[MAX_IP_LENGTH + 1];
而表
不是结构,而是指向结构的指针。那么,您的索引是错误的,您是在索引结构的成员,而不是数组
重写如下:
&(table.address)[i]
一切都是关于你的
的优先级高于&
,因此它基本上表示:
address[MAX_IP_LENGTH + 1];
而表
不是结构,而是指向结构的指针。那么,您的索引是错误的,您是在索引结构的成员,而不是数组
重写如下:
&(table.address)[i]
为什么你在编译中使用
.h
文件,你可以不使用cc
为什么你在编译中使用.h
文件,你可以不使用cc
文件