2

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?

Nandan Desai
  • 397
  • 3
  • 10
  • 3
    Maybe if you use C++. But in C, short of converting data to code in a non-portable way, no. – Garr Godfrey Apr 16 '18 at 08:05
  • 3
    No. C++ passes `this`, but does it implicitly. In pure C you must pass struct pointer youself – Alexander Dmitriev Apr 16 '18 at 08:06
  • Can I ask genuinely why 3 people upvoted this? There's so much wrong here. You never set that function pointer. It has the wrong argument list declared for either proposed signature. And you try to completely reimagine C as something that a basic understanding of the language makes clear it isn't, but for which another language exists. – underscore_d Apr 16 '18 at 09:59
  • 3
    @underscore_d I thought we could treat this piece of code as "pseudo-C" code, which nicely revealed sometimes it's quite difficult to simulate some C++ feature in C. So the wrongness of the code didn't matter that much. – Stan Apr 16 '18 at 10:33
  • Java's ArrayList is an array. You are trying to define a linked list, not an array. The rule of thumb regarding linked lists is very simple: never ever use them. – n. m. could be an AI Apr 16 '18 at 12:07
  • @underscore_d I have defined that structure just to give a context of what I am talking about and what my question is. That's why I have mentioned in the question to consider it as a pseudo code and not to tell me how to use a function pointer. My question boils down to, "I want to try out some "Object-Oriented style" in C. To what extent will C allow me to do it?" – Nandan Desai Apr 16 '18 at 13:04
  • Possible duplicate of [Can you write object-oriented code in C?](https://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c) – underscore_d Apr 16 '18 at 13:06
  • @underscore_d Nope. I had already looked at it. That's not the question I wanted to ask. My question was specific to whether `aL->add(element);` can be done or not. Whether C allows me to do something like that. – Nandan Desai Apr 16 '18 at 13:18
  • C doesn't allow you to do that. If it did, people would have shown that at the other question, wouldn't they? – underscore_d Apr 16 '18 at 13:19
  • @underscore_d "C doesn't allow you to do that". Well, this is what I wanted to know. Thanks. – Nandan Desai Apr 16 '18 at 13:35
  • @underscore_d check out the edit. I have elaborated the question. Do you still consider it as a duplicate? – Nandan Desai Apr 16 '18 at 14:05
  • 1
    Syntax of function calls has nothing to do with object orientation. You can write OO code in C if you want to. You cannot change the way function call syntax works, but again, syntax is largely irrelevant. – n. m. could be an AI Apr 16 '18 at 14:06

2 Answers2

1

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.

perror
  • 7,071
  • 16
  • 58
  • 85
  • 4
    Lots of confusion in this answer. OO is a program design method, it does not dictate details such as the syntax for updating private member variables. C does not have outspoken language support for OO, yet you can write OO programs in C just fine. `aL.add(element)` is by no means "more OO" than `add(aL, element)`. In both cases `aL` is the object, `add` is the member function and `element` is the argument passed. – Lundin Apr 16 '18 at 09:38
  • This is not the answer. The function pointer was not declared properly and any call to it is an UB. – 0___________ Apr 16 '18 at 09:51
  • That's not the answer in and of itself either; there are many other problems with the posted code. – underscore_d Apr 16 '18 at 09:59
0

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.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Is that `add()` pointer meant to take the instance as 1st argument? But then what is the point of it being an instance member? There's no indication that the OP needs the function to vary per instance, so then storing it per instance would be a waste of space. Besides, even if it were a virtual function, those are stored per-class structure, not per-instance. My concern is that giving the OP something that's as close to what they asked for as possible would be encouraging bad design. – underscore_d Apr 16 '18 at 13:36
  • @underscore_d we are not here to teach people (at least I am not) hoe to design programs. I have just noticed the UB. – 0___________ Apr 16 '18 at 14:04
  • "Using the standard C you cant find the object which contains this function pointer." This part of your answer was relevant and answered the my question. I will accept your answer if you edit it to retain the relevant part and elaborate. Thanks you! – Nandan Desai Apr 16 '18 at 14:34