What is void * userData exactly?
In the C function declaration, I saw this parameter definition:
void *userData
So what is it? My guess is that emptiness says it could be anything, or even nothing. Almost similar to id objective-c. It just lets you pass any data structure you like.
The star in front of userData says that the argument should be passed by reference.
So when you use this stuff in the body of a function, it usually needs to be stripped and dereferenced. So if I pass in a pointer to an instance of SomeClass I would understand the following:
SomeClass *myObj = (SomeClass*)userData;
In case I didn't have anything special, I would provide NULL as an argument.
Are my assumptions correct? Or have I misunderstood something?
a source to share
A void *
is a pointer to a structure of unknown type. You can think of this as a "pointer to anything". It is not the same as the Objective-C id, which is the type of any object. The identifier looks like this:
typedef struct objc_object {
Class isa;
} *id;
Usually in Objective-C (and I'm sure about C too), a framework can use some kind of callback to tell you something. This callback often takes a parameter containing the data you give it - perhaps the object that started the action and that should know that something happened. The parameter void *
allows the framework to define a callback that is independent of your code, so you can put whatever you like in the callback. (Of course, this means that you will also have to cast yours back void *userData
to the appropriate type.)
a source to share
void *
means a pointer to anything. So, you can pass anything "by reference" through an argument void *
. userData
indicates the name of the parameter (but this is just a suggestion, so it can vary depending on different APIs) that this argument is arbitrary user-supplied data. That is, the function in question will not do anything with it, it will just save it and give you some time back. For example, in GTK +, you can attach a callback to some event. You can pass your data in g_signal_connect
as an argument user_data
, and in the event handler GTK + will pass your data to you:
GtkWidget *button = gtk_button_new ();
const char *data = "Hello World;)";
g_signal_connect (button, "clicked", on_clicked, data);
(...)
void on_clicked (GtkWidget *widget, void * data)
{
const char *text = (const char *)data;
printf ("%s\n", text); // will print "Hello World;)"
}
a source to share
Without seeing the actual code, I am assuming the parameter belongs to the function that is doing the callback? If so, this is probably part of the context that you say you can set whatever you want. When a function calls your callback, it passes this context, which means you don't need a global data store.
a source to share
Your understanding is essentially correct, although it is called a pointer and not a link. This is usually a mechanism that allows users to "extend" the data structure by passing any type of data into a block. The example I have seen is used for tree display nodes in the GUI. The GUI adds a void pointer so that when the user clicks on a node in that tree, you can output more meaningful information about the node than just its name to your calling procedure. Since only you know what data type will be relevant for your application, the type is simply void *.
Also note that you are almost always responsible for memory management for userData.
a source to share