用C模块化数据结构与动态数据类型

用C模块化数据结构与动态数据类型

本文介绍了用C模块化数据结构与动态数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我即将大学C项目,我要求有模块化code为C允许它。基本上,我会.c文件和一些数据结构的相应的.h文件中,就像一个链表,二叉树,哈希表,无论...

For my upcoming university C project, I'm requested to have modular code as C allows it. Basically, I'll have .c file and a corresponding .h file for some data structure, like a linked list, binary tree, hash table, whatever...

使用链表作为一个例子,我有这样的:

Using a linked list as an example, I have this:

typedef struct sLinkedList {
    int value;
    struct sLinkedList *next;
} List;

但这种力量为类型 INT 并使用该链表库的用户将被强制直接更改库的源$ C ​​$ C。我想避免这种情况,我想避免需要改变库,使code尽可能模块化。

But this forces value to be of type int and the user using this linked list library would be forced to directly change the source code of the library. I want to avoid that, I want to avoid the need to change the library, to make the code as modular as possible.

我的项目可能需要使用一个链表整数列表,或者一些结构的列表。但我不会重复库文件/ code和相应地改变code。

My project may need to use a linked list for a list of integers, or maybe a list of some structure. But I'm not going to duplicate the library files/code and change the code accordingly.

我该如何解决呢?

推荐答案

不幸的是,解决这个没有简单的方法。最常见的,纯C的方法来这种情况是使用无效* ,和值复制到由你分配为指针内存。这使得使用棘手,不过,很容易出错。

Unfortunately, there is no simple way to solve this. The most common, pure C approach to this type of situation is to use a void*, and to copy the value into memory allocated by you into the pointer. This makes usage tricky, though, and is very error prone.

这篇关于用C模块化数据结构与动态数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:10