Nandan Desai
Nandan Desai

Reputation: 397

Function pointer inside a Structure in C

I am trying to implement an ArrayList (a dynamic array used in java) using C in Object-Oriented style. I have defined the following struct. (Consider this as a pseudo code)

struct ArrayList{
    /*structure member declarations...
     ...
    */
    void (*add)(); /*function pointer which 
points to the function which will add a new element at the end of the list*/
};

/*Function to create an ArrayList "object"*/
struct ArrayList* newArrayList(){
    /*prepare an ArrayList "object" and return*/
    return arrayList;
}

My question is, is it possible to do something like

struct ArrayList* aL=newArrayList();
aL->add(element); /*I want to do it like this*/
aL->add(&aL, element); /*And not like this*/

I don't want to pass the reference of ArrayList again. I thought of using a static struct ArrayList* variable inside the implementation of add function so that I can initialize it once and it will be used in the subsequent function calls, but then I thought it will create a mess when I create

struct ArrayList* aL2=newArrayList();
aL2->add(element); 

I know we can write Object-Oriented code in C to some extent. But is it possible to do aL->add(element); like, the way we access a method in Object-Oriented language?

Upvotes: 2

Views: 424

Answers (2)

perror
perror

Reputation: 7386

You are trying to apply object-oriented paradigms to C which, in this particular case, is doomed to fail miserably.

In fact, the object-oriented idiom aL->add(element) is just a short-end for add(aL, element). For example, if you take Python, both syntax can be used. And, C++ has internal mechanisms that allow to consider the aL object as the first parameter of the method add().

I would first tell you to accept that C does not provide built-in object-oriented syntax and if you want to write aL->add(element) in C, then write add(aL, element).

You will much better match the C spirit and, learn that aL->add(element) is just a syntax idiom telling you that the first argument (the object itself) is a special argument and nothing more.

Upvotes: 1

0___________
0___________

Reputation: 67476

your function pointer definition is wrong as the compiler does not know what parameters it takes and assumes them as ints.

For example you need to let the compiler know:

struct ArrayList;
struct ArrayList{
    struct ArrayList* prev;
    struct ArrayList* next;
    void* element; /*data*/
    void (*add)(struct ArrayList *, struct ArrayList); /*function pointer which 
points to the function which will add a new element at the end of the list*/
};

Now the compiler knows that it has to pass pointer & the structure itself.

Answering the second part of your question you can pass only the new element. You just need to find the last element in your list. But this assumes that you have only one list.

Using the standard C you cant find the object which contains this function pointer.

Upvotes: 0

Related Questions