通过函数调用初始化结构的结构

initialize a struct of structs through function calls

无法找出用于在结构中初始化结构的嵌套取消引用。我终于弄清楚了通过对 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 = "the data at the address foo"

&foo = "the address of the data foo"

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

printf("write: %s" InodeTableString);

相对于

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

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

同样在访问 foo 指向的结构的成员 bar 时,最好使用

foo->bar

换句话说

foo->bar = "the element bar from the struct that is pointed to by foo"

你可以避免整个 "do I use * or & problem altogether" 因为以下是正确的

foo->bar = (*foo).bar

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

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