C中的通用数组 [英] general array in C
问题描述
我想创建一个可以容纳所有类型的通用数组结构。
这样的东西:
struct ARRAY
{
void ** array;
size_t size;
};
很好地存储指向其中结构的指针但是什么是一个好的方法
能够存储指向结构和原始类型的指针吗?
Hi,
I want to create a general array structure which can hold all types.
Something like this:
struct ARRAY
{
void **array;
size_t size;
};
Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?
推荐答案
Cyn写道:
Cyn wrote:
我想要创建一个通用的数组结构,可以容纳所有类型。
这样的东西:
struct ARRAY
{
void **数组;
size_t size;
};
很好地存储指向其中结构的指针但是什么是一个很好的方法
能够存储指向结构和原始类型的指针吗?
Hi,
I want to create a general array structure which can hold all types.
Something like this:
struct ARRAY
{
void **array;
size_t size;
};
Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?
typedef联盟{
void * Vptr;
int Int;
short短;
char Char;
long Long;
long long LLong;
double double;
long double LDouble;
float Float;
} CELL;
struct ARRAY {
CELL *数组;
size_t size;
};
struct ARRAY * newArray(size_t siz)
{
//分配并返回ARRAY
//。 ..
}
int main(无效)
{
ARRAY * a;
a = newArray(1024);
a.array [23] .Float = 2.13f;
a.array [24] ] .Vptr = malloc(267);
a.array [25] .LLong = 533777899LL;
a.array [26] .Char =''F'' ;
a.array [27] .Vptr ="这是一个示例字符串" ;;
}
我会使用类型字段来避免错误,通过更改以下内容存储哪些类型存储在数组中:
struct ARRAY {
CELL * array;
cha r * CellTypes;
size_t size;
};
#define VPTR 1
#定义INT 2
#define SHORT 3
....等
并存储每个单元格的类型相邻的字符数组。
jacob
typedef union {
void *Vptr;
int Int;
short Short;
char Char;
long Long;
long long LLong;
double Double;
long double LDouble;
float Float;
} CELL;
struct ARRAY {
CELL *array;
size_t size;
};
struct ARRAY *newArray(size_t siz)
{
// Allocates and returns an ARRAY
// ...
}
int main(void)
{
ARRAY *a;
a = newArray(1024);
a.array[23].Float = 2.13f;
a.array[24].Vptr = malloc(267);
a.array[25].LLong = 533777899LL;
a.array[26].Char = ''F'';
a.array[27].Vptr = "This is an example string";
}
I would use a type field to avoid mistakes, storing which
type is stored in tha array by changing:
struct ARRAY {
CELL *array;
char *CellTypes;
size_t size;
};
#define VPTR 1
#define INT 2
#define SHORT 3
.... etc
and you store the type of each cell in the adjacent array of characters.
jacob
Cyn写道:
Cyn wrote:
我想创建一个可以容纳所有类型的通用数组结构。
这样的东西:
struct ARRAY
{
void ** array;
size_t size;
};
很好地存储指向它中结构的指针但是什么是一个很好的方法
能够存储指向结构和原始类型的指针吗?
Hi,
I want to create a general array structure which can hold all types.
Something like this:
struct ARRAY
{
void **array;
size_t size;
};
Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?
您正在尝试重新发明C ++对象。如果你想做很多
这个,最好让C ++编译器和运行时库担心
关于这一切。
您可能想要包括的其他一些字段:每个元素的大小,元素类型的
代码,维度的数量,可能是
下一个对象(用于垃圾收集)
You''re trying to re-invent the C++ object. If you want to do much of
this, it''s best to let the C++ compiler and run-time library worry
about all this.
Some other fields you may want to include: the size of each element, a
code for the type of element, the number of dimensions, maybe a link to
the next object (for garbage collection)
Ancient_Hacker写道:
Ancient_Hacker wrote:
Cyn写道:
Cyn wrote:
>>
我想创建一个可以容纳所有类型的通用数组结构。
类似于这个:
struct ARRAY
{
无**阵列;
size_t size;
};
工作得很好存储指向它中的结构的指针,但是什么是能够存储指向结构和原始类型的指针的好方法呢?
>>Hi,
I want to create a general array structure which can hold all types.
Something like this:
struct ARRAY
{
void **array;
size_t size;
};
Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?
您正在尝试重新发明C ++对象。如果你想做很多
这个,最好让C ++编译器和运行时库担心
关于这一切。
You''re trying to re-invent the C++ object. If you want to do much of
this, it''s best to let the C++ compiler and run-time library worry
about all this.
????
见上面的帖子。为什么你需要一个C ++对象?要做到这一点吗?
你能解释一下吗?
谢谢
????
See my post above. Why do you need a "C++ object" to do that?
can you explain?
Thanks
Some您可能想要包含的其他字段:每个元素的大小,元素类型的元数据,维数,可能是下一个对象的链接
(用于垃圾收集)
Some other fields you may want to include: the size of each element, a
code for the type of element, the number of dimensions, maybe a link to
the next object (for garbage collection)
这篇关于C中的通用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!