#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// タイプ識別子
typedef enum {
VTP_CHAR = 0,
VTP_INT,
//…
} V_TYPE;
// タイプごとのサイズ
enum {
TSZ_CHAR = sizeof(char),
TSZ_INT = sizeof(int),
//…
};
// 本体データ
typedef struct _VARIANT {
char m_name[256];
V_TYPE m_type;
int m_size; // = memsize = sizeof(…) * n
void *m_data;
} VARIANT;
VARIANT *mk_arr(char *name, char *type, int n)
{
VARIANT *p_tmp;
int sz;
V_TYPE tp;
// check
if (0 == strcmp(type
, "char")) { sz = TSZ_CHAR;
tp = VTP_CHAR;
} else if (0 == strcmp(type
, "int")) { sz = TSZ_INT;
tp = VTP_INT;
//…} else if (0 == strcmp(type, "……")) {
//… ;
} else {
return NULL;
}
// alloc
if (NULL
== (p_tmp
= malloc(sizeof(VARIANT
)))) { return NULL;
}
// set
p_tmp->m_type = tp;
p_tmp->m_size = sz * n;
if (0 == (p_tmp
->m_data
= malloc(p_tmp
->m_size
))) { return NULL;
} else {
return p_tmp;
}
}
void del_arr(VARIANT * p)
{
}
int main()
{
VARIANT *p;
char name[] = "str";
char type[] = "int";
int n = 10;
if (NULL == (p = mk_arr(name, type, n))) {
return 1;
} else {
printf("作成しました %s %s[%d]\n", type
, name
, n
); }
del_arr(p);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPHN0cmluZy5oPgoKLy8g44K/44Kk44OX6K2Y5Yil5a2QCnR5cGVkZWYgZW51bSB7CiAgICBWVFBfQ0hBUiA9IDAsCiAgICBWVFBfSU5ULAogICAgLy/igKYKfSBWX1RZUEU7CgovLyDjgr/jgqTjg5fjgZTjgajjga7jgrXjgqTjgroKZW51bSB7CiAgICBUU1pfQ0hBUiA9IHNpemVvZihjaGFyKSwKICAgIFRTWl9JTlQgPSBzaXplb2YoaW50KSwKICAgIC8v4oCmCn07CgovLyDmnKzkvZPjg4fjg7zjgr8KdHlwZWRlZiBzdHJ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