What are these complex members within a struct?

Asked

Viewed 90 times

5

I have a question in a struct on the last lines with uchar and void which is quite different from what I know.

Why many programmers use underline in the names of structs, variables etc...

struct WNMEnv_
{
    ObjectHeap *heap;
    int32 intfVersion;

    uchar (*IsInstanceOf) (WNMEnv *env, WObject obj, WClazz *clazz);

    void (*ThrowException) (WNMEnv *env, const char *name, const char *msg);
};
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

2 answers

6

These lines are pointers to functions, I have answered with more detail in Use a void function as a parameter of another. The void and the uchar are return types as occurs in any other function. And the fact of being within a struct does not change anything, only you have variables to save the pointer to the function, but are still normal functions, only the access will be indirect. Has a another example. And another example.

This is a form of indirect for calling a function, then you create a pointer to an address where the function is (the body of the function will be elsewhere), then you can store in a variable that pointer value, in this case the variable is within a struct. Changing the value will access a different function, and this is powerful to give flexibility and let decide what to call at runtime.

Actually this mechanism used in this way is something like polymorphism that you might know about object orientation. It is possible to program OO in C using precisely this capability. When you use a language natively OOP internally this is what it does, only you don’t see, in C you see and manipulate in hand.

If it is not clear, the second block of parentheses are the function parameters. So in this case taking the example you can at some point in your code tell what is the function to be called by placing its address in the variable ThrowException inside the object you are manipulating that is of the WNMEnv_. So when you create the object you configure what should be called. Somewhere the called function will come from what is in ThrowException, probably this call is already within the infrastructure of what you are using, is part of the internal implementation of this API, you just configure what will be run.

If you know any other more abstract language you can compare with lambda or anonymous function. Internally that’s how it works, even in those languages. We also call it callback.

To better understand it would be interesting to take a chunk of code that has the use of this or an object example created and configured for use of this.

That’s another question but come on, people use _ to avoid any conflict of new, in general the team chooses a convention that names with it have a special meaning, so do not mix with other names that may need to be equal.

  • Thanks Maniero, just one more question I was wondering... Inside this struct, there are more items, one of them is this: char *(*Getstringchars) (Wnmenv *env, Wobject str, int32 flags); Here: *(*Getstringchars) means pointer to the pointer to the pointer of Getstringchars?

0

Just to add: struct WNMEnv_ This is not an underscore. use will make a lot of difference in external documents for example read and write in a word file in your project.

The underscore is on the side of the "character_".

The underline is underneath the "character" (* Does anyone know how to underline a word here in stackoverflow? I had to improvise rsrsrs)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.