私は単純な構造を持っています:
typedef struct {
void *things;
int sizeOfThings;
} Demo;
things は、文字列や整数など、個々の「もの」の配列を含むことを意図しています。私はそれへのポインタを作成します:
Demo * Create(int value) {
Demo *d = malloc(sizeof(Demo));
if (d != NULL) {
d->sizeOfThings = value;
d->things = malloc(20 * value); // We'll have a max of 20 things
}
}
value
たとえば、int の配列の場合は sizeof(int) です。
別の関数で d->things に何かを挿入したい場合 (少なくとも最初のスロットに追加するだけではなく、位置管理は他の場所で行われると仮定します):
char * thing = "Me!";
strncpy(d->things[0], &thing, d->sizeOfThings);
strncpyエリアを回ります
test.c:10: warning: pointer of type ‘void *’ used in arithmetic
test.c:10: warning: dereferencing ‘void *’ pointer
test.c:10: error: invalid use of void expression
関数を一般化する方法として void* の使用を理解しようとしています。に何か問題があるのではないかと思いd->things[0]
ます。