gpt4 book ai didi

c - 在单独的头文件中定义的结构

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:54 25 4
gpt4 key购买 nike

LMlib.h

#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

#include <stdio.h>
#include <math.h>
#include "LMlib.h"

void ReadForwardingTable(FILE* f,ForwardingTableRow * table)
{
int i;
for(i=0;i<MAX_TABLE_ROWS;i++)
{
fscanf(f,"%s %d %d",&table.address[i],&table.subnetMask[i],&table.interface[i]);
}


}

编译器命令:

cc LMlib.c LMlib.h main.c -lm

错误:

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

我做错了什么?

最佳答案

你有三个问题:第一个是你没有正确使用数组索引。 table 变量是数组,不是结构成员:

fscanf(f, "%s %d %d",
table[i].address,
&table[i].subnetMask,
&table[i].interface);

第二个问题与你的问题无关,但可能会给以后带来麻烦。这是你拥有的包括守卫。 #endif 应该位于文件的末尾,否则您只保护单个 #define 而没有其他内容。

第三个也是最严重的问题是您在 address 字段中的字符太少了。 IP 地址的最大长度为 15,这是正确的,但如果您想将其视为字符串,则还需要为字符串终止符留出空间。声明为

address[MAX_IP_LENGTH + 1];

应该没问题。

关于c - 在单独的头文件中定义的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13271870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com