0

I have never programmed in C, so please can anyone tell me what does this construction do:

void funcName(void * self)
{ ... }

as far as I understood the funcName receives the pointer to the unknown data and this function receives nothing

but why there is a void * self

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

1 Answers1

4

This is a function that takes a void pointer - a pointer without a specific type. Void pointer type lets you pass a pointer to any data type to a function that is declared like that. The function will have to cast the pointer to an appropriate type before reading/writing the data from it, or pass the pointer on to other functions that take void*.

Any pointer can be cast to void*, implicitly or explicitly. For example, you can call funcName like this:

int *ptr = malloc(100*sizeof(int));
funcName(ptr); // No error

You can then call the same function with a different pointer type:

struct mystruct_t *ptr = malloc(100*sizeof(mystruct_t));
funcName(ptr); // Again, no error

Judging from the name of the pointer, self, it is likely that the function is trying to emulate object-oriented style of programming with constructs available in C. Instead of passing the pointer to data implicitly the way member-functions do, this style passes a pointer to data explicitly. The pointer is often called this or self.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523