1

I have this structure :

typedef struct        s_hashmap
{
    t_hashmap_elem    **array_elements;
    size_t            array_size;
    void              *(*get)(struct s_hashmap **, void *);
    void              (*add)(struct s_hashmap **, t_hashmap_elem *);
}                     t_hashmap;

Can I access to array_elements when i'm in get function pointer without pass my variable as parameter like this :

h->get(&h, &key); // h is the t_hashmap variable.

if this is not possible please explain me an other way to do it.

thanks in advance.

  • What do you mean by *when I'm in get function pointer*? If you are in the implementation of the `get` function, you receive a `t_hashmap**` handler. I cannot understand the need for a double indirection, but you should pass that whatever call to the `get` function pointer. – chqrlie Jan 22 '16 at 22:35
  • Then drop this style, it does not help with readability. – chqrlie Jan 22 '16 at 22:37
  • I asked if i can just call the get function like this : h->get(&key); and access other variables of the structure h. –  Jan 22 '16 at 22:38
  • OK, I wasn't sure... You cannot do this in C, and be aware that in C++, the methods do not receive a doubly indirected pointer, but just a pointer to the instance as extra implicit initial argument. – chqrlie Jan 22 '16 at 22:40
  • btw, do they use the same style at Epitech? – chqrlie Jan 22 '16 at 22:47
  • Yes i think it's the same –  Jan 24 '16 at 11:01

2 Answers2

2

No you can not. There are no methods of user-defined types in C. You have to pass a pointer (or pointer to pointer depending on the parameter declaration) to an object of the structure if you want to change it.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    @ClémentJean C is not an OOP language because you may not define behaviour of your types. However you can use the OOP style of programming in C.:) – Vlad from Moscow Jan 22 '16 at 22:31
  • I know it's not OOP, it's just because i want to play with limits of C :) –  Jan 22 '16 at 22:33
  • @ClémentJean It isn't *not* OOP, it's just not the way most *other* languages do OOP. It's a perfectly legitimate way to program in C. – trent Jan 22 '16 at 22:49
  • @trentcl - _It isn't not OOP_? You have an extra _not_ in there I think. C is clearly not an OOP. It can be used to implement OOP, but it is not. – ryyker Jan 22 '16 at 23:00
  • @ryyker I said exactly what I meant. This is what OOP looks like in C. OOP is a programming style, not a language feature. – trent Jan 22 '16 at 23:24
  • @trentcl - The term OOP conveys a special set of programming language features That exist in _[these languages](https://en.wikipedia.org/wiki/List_of_object-oriented_programming_languages)_, but not _[in C](http://programmers.stackexchange.com/a/113541/143513)_ – ryyker Jan 22 '16 at 23:34
  • @ryyker "Object-oriented programming" is a programming style. [You can do OOP in C](https://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c), it just looks ugly because C lacks features specifically geared for OOP. That's pretty much what this question is about, no? – trent Jan 23 '16 at 01:05
2

Nope. C functions are not methods: there is no mechanism for a function to discover the object through which it was called. If this feature is desirable to you, you should consider using a language other than C.

You pretty much nailed the "other way to do it" in your question. But note that in C there is rarely any reason for a struct to own a function pointer, so you might as well just call the function by its proper name:

hashmap_get(&h, &key);

There are uses for function pointers in C, but they aren't very good for emulating object methods, because all the magic that languages like Java do for you (figuring out which method should be called on a given object, and passing in a reference to the object) has to be done explicitly in C.

trent
  • 25,033
  • 7
  • 51
  • 90