我如何在指针数组中分配指针值
How do i assign pointer value inside array of pointers
我有这个错误:
ID3.c:71:53: 错误:从类型 't_object *' {aka ' 分配给类型 't_object' {aka 'struct object'} 时类型不兼容结构对象 *'}
71 | (objects->v[sizeof(void *) * i]) = t_object_ctor();
| ^~~~~~~~~~~~~
我试过这个:
&(objects->v[sizeof(void *) * i]) = t_object_ctor();
并尝试了这个
*(objects->v[sizeof(void *) * i]) = t_object_ctor();
这给出了同样的错误
*((objects->v)+[sizeof(void *) * i]) = t_object_ctor();
这是错误所在的函数:
t_objects *t_objects_ctor(){
t_objects *objects =malloc(sizeof(t_objects));
objects->size = 0;
objects->y = 0;
objects->x = 0;
objects->x_size= malloc(sizeof(size_t) * RESONABLENUMBER);
objects->v= malloc(sizeof(void *) * RESONABLENUMBER);
for (int i = 0; i < RESONABLENUMBER; ++i) {
*((objects->v) + sizeof(void *) * i) = t_object_ctor();
}
objects->b_size = 0;
objects->b = NULL;
return objects;
}
这是我分配指针的函数:
t_object *t_object_ctor(){
t_object *object =malloc(sizeof(t_object));
object->s = 0;
object->x = NULL;
object->y = 0;
return object;
}
试试这个:
((t_object **)objects->v)[i] = t_object_ctor();
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RESONABLENUMBER 16
typedef struct t_objects {
int size;
int x, y;
void *x_size;
void *v;
int b_size;
char *b;
} t_objects;
typedef struct t_object {
int s;
void *x;
int y;
} t_object;
t_object *t_object_ctor()
{
t_object *object = malloc(sizeof(t_object));
object->s = 0;
object->x = NULL;
object->y = 0;
return object;
}
t_objects *t_objects_ctor()
{
t_objects *objects = malloc(sizeof(t_objects));
objects->size = 0;
objects->y = 0;
objects->x = 0;
objects->x_size = malloc(sizeof(size_t) * RESONABLENUMBER);
objects->v = malloc(sizeof(void *) * RESONABLENUMBER);
for (int i = 0; i < RESONABLENUMBER; ++i) {
((t_object **)objects->v)[i] = t_object_ctor();
}
objects->b_size = 0;
objects->b = NULL;
return objects;
}
int main()
{
t_objects_ctor();
return 0;
}
我有这个错误:
ID3.c:71:53: 错误:从类型 't_object *' {aka ' 分配给类型 't_object' {aka 'struct object'} 时类型不兼容结构对象 *'} 71 | (objects->v[sizeof(void *) * i]) = t_object_ctor(); | ^~~~~~~~~~~~~
我试过这个:
&(objects->v[sizeof(void *) * i]) = t_object_ctor();
并尝试了这个
*(objects->v[sizeof(void *) * i]) = t_object_ctor();
这给出了同样的错误
*((objects->v)+[sizeof(void *) * i]) = t_object_ctor();
这是错误所在的函数: t_objects *t_objects_ctor(){ t_objects *objects =malloc(sizeof(t_objects));
objects->size = 0;
objects->y = 0;
objects->x = 0;
objects->x_size= malloc(sizeof(size_t) * RESONABLENUMBER);
objects->v= malloc(sizeof(void *) * RESONABLENUMBER);
for (int i = 0; i < RESONABLENUMBER; ++i) {
*((objects->v) + sizeof(void *) * i) = t_object_ctor();
}
objects->b_size = 0;
objects->b = NULL;
return objects;
}
这是我分配指针的函数:
t_object *t_object_ctor(){
t_object *object =malloc(sizeof(t_object));
object->s = 0;
object->x = NULL;
object->y = 0;
return object;
}
试试这个:
((t_object **)objects->v)[i] = t_object_ctor();
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RESONABLENUMBER 16
typedef struct t_objects {
int size;
int x, y;
void *x_size;
void *v;
int b_size;
char *b;
} t_objects;
typedef struct t_object {
int s;
void *x;
int y;
} t_object;
t_object *t_object_ctor()
{
t_object *object = malloc(sizeof(t_object));
object->s = 0;
object->x = NULL;
object->y = 0;
return object;
}
t_objects *t_objects_ctor()
{
t_objects *objects = malloc(sizeof(t_objects));
objects->size = 0;
objects->y = 0;
objects->x = 0;
objects->x_size = malloc(sizeof(size_t) * RESONABLENUMBER);
objects->v = malloc(sizeof(void *) * RESONABLENUMBER);
for (int i = 0; i < RESONABLENUMBER; ++i) {
((t_object **)objects->v)[i] = t_object_ctor();
}
objects->b_size = 0;
objects->b = NULL;
return objects;
}
int main()
{
t_objects_ctor();
return 0;
}