Can't figure out nested dereferencing for initializing a struct within a struct. I finally figured out initializing a struct through function calls for the Inode struct, but I can't seem to translate that to initializing a struct of structs through function calls for the Inodetable struct (which is a struct of Inode structs).
#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; }