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.

6
  • 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. Commented Jan 22, 2016 at 22:35
  • Then drop this style, it does not help with readability. Commented Jan 22, 2016 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. Commented Jan 22, 2016 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. Commented Jan 22, 2016 at 22:40
  • btw, do they use the same style at Epitech? Commented Jan 22, 2016 at 22:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@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.:)
I know it's not OOP, it's just because i want to play with limits of C :)
@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.
@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 I said exactly what I meant. This is what OOP looks like in C. OOP is a programming style, not a language feature.
|
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.

Comments

Your Answer

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