简体   繁体   中英

pointer to function that return pointer to struct

I'm trying to code an kind of ArrayList (struct) defined as follow:

typedef struct {
    int reservedSize; // reserved size
    int size; // Size of list
    void ** pElements; //pointer to list elements
    void (*deleteArrayList)();
    void (*add)();
    void (*remove)();
    void (*set)();
    void* (*get)();
    int (*indexOf)();
    int (*len)();
    void (*clear)();
    void (*push)();
    void* (*pop)();
    int (*contains)();
    int (*isEmpty)();
    //Extras
    int (* containsAll)();
    struct ArrayList* (*clone)(); //trouble1
    struct ArrayList* (*subList)();//trouble2 
} ArrayList;

As you can see, two elements type int , and pointers to functions, but I only have troubles with the last two. I have an initializator as follows:

ArrayList* al_newArrayList(void) { 
    ArrayList* list;
    list = (ArrayList *) malloc (sizeof(ArrayList));
    list->size = 0;
    list->pElements = malloc(sizeof(void *) * 10);
    list->reservedSize = 10;
    list->add = al_add;
    list->deleteArrayList = al_deleteArrayList;
    list->remove = al_remove;
    list->set = al_set;
    list->get = al_get;
    list->indexOf = al_indexOf;
    list->len = al_len;
    list->clear = al_clear;
    list->push = al_push;
    list->pop = al_pop;
    list->contains = al_contains;
    list->isEmpty = al_isEmpty;
    list->containsAll = al_containsAll;
    list->clone = al_clone;//trouble1
    list->subList = al_subList;//trouble2
    return list;
}

The program works but have a runtime error, and I have warnings about wrong referencing at:

list->clone = al_clone;//trouble1
list->subList = al_subList;//trouble2

Even those functions works but the program is closed. Do you have any idea? Wrong syntax?

typedef struct {
    int reservedSize; // reserved size
    int size; // Size of list
    // ...
    struct ArrayList* (*clone)(); //trouble1
    struct ArrayList* (*subList)();//trouble2 
} ArrayList;

By the time the compiler gets to the "trouble" lines there is no type named struct ArrayList .

There is a struct type with no tagname being defined.

At the end of that definition, there will be a type named ArrayList (a struct type with no tagname) defined.

Solution:

define the type struct ArrayList

typedef struct ArrayList {             // define type with tagname
    int reservedSize; // reserved size
    int size; // Size of list
    // ...
    struct ArrayList* (*clone)(); //trouble1
    struct ArrayList* (*subList)();//trouble2 
} ArrayList;

All thanks goes to @pmg who points me.

typedef struct ArrayList ArrayList;
struct ArrayList{
    int reservedSize; // reserved size
    int size; // Size of list
    void ** pElements; //pointer to list elements
    void (*deleteArrayList)();
    void (*add)();
    void (*remove)();
    void (*set)();
    void* (*get)();
    int (*indexOf)();
    int (*len)();
    void (*clear)();
    void (*push)();
    void* (*pop)();
    int (*contains)();
    int (*isEmpty)();
    //Extras
    int (* containsAll)();
    struct ArrayList* (*clone)(); //trouble1
    struct ArrayList* (*subList)();//trouble2 
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM