开发者

Structs, pointers, and trees in C

For our final semester project, everyone in my Operating Systems class has been tasked with implementing a pseudo 开发者_高级运维"linux filesystem". The idea is to simulate handling files, folders, changing directories, and so forth.

I dislike having to work with Strings and pointers when I program in C, and unfortunately for my peace-of-mind, this project looks to involve both. Because I am relatively uncomfortable with pointers, I was hoping I could get a sanity check that my backend implementation of the underlying tree structure is sound.

typedef struct floorNode
{
    char floorName[30]; //the name of the tree node
    struct floorNode *parentPointer; //this is a pointer to the parent node. Null for the root node.
    struct floorNode *childPointers[10]; //this is an array holding pointers to up to 10 child nodes. 
    char fileArray[10][30]; //this is an array of 10 'files', each of up to length 30.
                            //for this assignment, strings are the only type of "file"

} floorNode;

Is this the proper way to implement a tree in C?


That is more or less the proper data type.

I'm concerned about fileArray[][]. I don't think it is needed, unless I'm misunderstanding its purpose. To get floorName of the children, instead traverse childPointers[] to obtain the name in the children.

Something to consider if the nodes have 30 character strings is to make the storage for all of them a little bigger, 31 in this case, so that a trailing NUL is always present and no special yucky handling is needed to distinguish between a 30-character string without a NUL and all the shorter strings which have one.


You probably want a linked list of children. You definitely don't want arrays of pointers for this. You should also think about how to know if a file is actually a directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜