0

i have this error:

ID3.c:71:53: error: incompatible types when assigning to type ‘t_object’ {aka ‘struct object’} from type ‘t_object *’ {aka ‘struct object *’} 71 | (objects->v[sizeof(void *) * i]) = t_object_ctor(); | ^~~~~~~~~~~~~

i tried this:

&(objects->v[sizeof(void *) * i]) = t_object_ctor();

and tried this

*(objects->v[sizeof(void *) * i]) = t_object_ctor();

and this gives the same error

*((objects->v)+[sizeof(void *) * i]) = t_object_ctor();

this is the funcion the error is in: 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;
}

this is the function im assigning pointers from:

t_object *t_object_ctor(){
    t_object *object =malloc(sizeof(t_object));
    
    object->s = 0;
    object->x = NULL;
    object->y = 0;
    
    return object;
    
}
6
  • if objects is t_object * objects[] then it should just be objects[i] = t_object_ctor(); Commented Apr 3, 2022 at 8:19
  • objects is not t_object Commented Apr 3, 2022 at 8:22
  • can you post a full code example? Commented Apr 3, 2022 at 8:26
  • objects is a pointer on struct that is named t_objects with 'v' that is a pointer to t_object (which i want to use as a array) Commented Apr 3, 2022 at 8:27
  • i dont quite understand what you mean by full code example Commented Apr 3, 2022 at 8:30

1 Answer 1

1
Try this:
((t_object **)objects->v)[i] = t_object_ctor();
Full code like this:
#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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

If you declare void* v as t_object** v you wouldn't need to make the big cast in the for-loop
this seems to work as i intended, thanks
if i declare t_object* v as t_object** v it does break and does not compile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.