C - 通过函数调用初始化结构体



无法找出用于初始化结构内结构的嵌套取消引用。我终于想出了通过对 Inode 结构的函数调用来初始化结构,但我似乎无法将其转换为通过对 Inodetable 结构(它是 Inode 结构的结构(的函数调用来初始化结构结构。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int POINTERS_PER_INODE = 5;
int Total_Inodes = 64;
struct Inode {
int  valid;/* 0 == invalid, 1 == valid*/
int size;
int Blocks [5];
};
struct InodeTable InodeTable;
int InodeToString(char * InodeString, struct Inode iNode){
char * blockBuffer;
sprintf(InodeString, "%d", iNode.valid);
int i;
for (i = 0; i < POINTERS_PER_INODE; i++){
blockBuffer = malloc(8);
sprintf(blockBuffer, "%d", iNode.Blocks[i]); //no valid pointers yet
strcat(InodeString,blockBuffer);
free(blockBuffer);
}
return 0;
}

int initializeInode(struct Inode * iNode){
int i;
for (i = 0; i < POINTERS_PER_INODE; i++){
iNode->Blocks[i] = -1; //no valid pointers yet
}
iNode->valid = 0; //initialized as invalid inode
return 0;
}
int initializeInodeTable(struct InodeTable * ITable){
char * InodeTableString;
char * inodeString;
InodeTableString = malloc(sizeof(struct Inode) * 64);
memset(InodeTableString, 0 , sizeof(struct Inode) * 64);

for (int i = 0; i < Total_Inodes; i++){
inodeString = malloc(sizeof(struct Inode));
memset(inodeString, 0 , sizeof(struct Inode));
initializeInode(&ITable.InodeTable[i]);
InodeToString(inodeString, &ITable.InodeTable[i]);
strcat(InodeTableString,inodeString);
free(inodeString);
}

printf("write: %s"InodeTableString);
free(InodeTableString);
return 0;
}
int main() {
struct InodeTable iNodeTable[64];
initializeInodeTable(&iNodeTable);

return 0;
}

使用 gcc 编译代码(在 Mac 上技术上是 clang (会给出以下错误

Inode.c:52:30: error: member reference type 'struct InodeTable *' is a pointer; did you mean to
use '->'?
initializeInode(&ITable.InodeTable[i]);
~~~~~~^
->
Inode.c:52:30: error: incomplete definition of type 'struct InodeTable'
initializeInode(&ITable.InodeTable[i]);
~~~~~~^
Inode.c:15:8: note: forward declaration of 'struct InodeTable'
struct InodeTable InodeTable;
^
Inode.c:53:41: error: member reference type 'struct InodeTable *' is a pointer; did you mean to
use '->'?
InodeToString(inodeString, &ITable.InodeTable[i]);
~~~~~~^
->
Inode.c:53:41: error: incomplete definition of type 'struct InodeTable'
InodeToString(inodeString, &ITable.InodeTable[i]);
~~~~~~^
Inode.c:15:8: note: forward declaration of 'struct InodeTable'
struct InodeTable InodeTable;
^
Inode.c:59:23: error: expected ')'
printf("write: %s"InodeTableString);
^
Inode.c:59:11: note: to match this '('
printf("write: %s"InodeTableString);
^
Inode.c:65:31: error: array has incomplete element type 'struct InodeTable'
struct InodeTable iNodeTable[64];
^
Inode.c:15:8: note: forward declaration of 'struct InodeTable'
struct InodeTable InodeTable;
^
Inode.c:15:19: error: tentative definition has type 'struct InodeTable' that is never completed
struct InodeTable InodeTable;
^
Inode.c:15:8: note: forward declaration of 'struct InodeTable'
struct InodeTable InodeTable;
^
7 errors generated.

因此,您应该立即注意到您没有定义 InodeTable。也许这就是为什么您无法初始化第二个结构的原因。

另外,我认为你的意思是放*而不是&

*foo="地址foo处的数据">

&foo="数据foo的地址">

需要修复的另一件事是你把

printf("write: %s" InodeTableString);

而不是

printf("write: %s", InodeTableString);

如果您使用尝试使用的语法,则有多个参数

此外,当访问由foo指向的结构的成员bar

,最好使用
foo->bar

换句话说,在

foo->bar="从foo指向的结构中bar的元素">

你可以避免整个"我是否完全使用*或&问题",因为以下情况是正确的

foo->bar=(*foo).bar

但是,以下是一个可能的错误

(*foo).bar!=(&foo).bar

最新更新