extern struct declaration in C - c

I'm fairly new to C, hence could someone please help me understand the below struct declaration?
extern struct server_module* module_open (const char* module_path);
Per my understanding, module_open is pointer to the struct server_module, however, didn't understand the last part of the statement i.e. (const char* module_path)

extern struct server_module* module_open (const char* module_path); declares module_open to be a function taking a parameter named module_path of type const char * and returning a struct server-module *.

module_open is a function which returns pointer to struct server_module
and const char* module_path is input argument type. Means function takes character string as an input
extern keyword is used to tell compiler that symbol is exist in different file

Related

In there any difference between *<variable> and <variable>* in C? [duplicate]

This question already has an answer here:
Differences between pointer declaration [duplicate]
(1 answer)
Closed 2 years ago.
in the following function declaration
my_struct* create(char *name);
Is there any difference in the meaning of * in my_struct* and *name?
I understand that char *name means that we will pass a char pointer to the function my_struct called name. I also get that the function will return a pointer to (memory address of) something. What I don't understand is why my_struct* and not *my_struct?
In this declaration
my_struct * create(char *name);
the name of the function is create. The function has one parameter of the type char *, And the function has the return type my_struct *. That is it returns a pointer to an object of the type my_struct.
Is there any difference in the meaning of * in my_struct* and *name
my_struct is a type specifier. So my_struct * is a pointer type. name is identifier that denotes the name of a parameter. The type of the parameter (identifier) name is char *.
Pay attention to that these declarations of the parameter are the same
char* name
char * name
char *name
that is the type of the parameter is char * and the name of the parameter is name.
In a function declaration that is not at the same time its definition names of parameters may be omitted.
So the above function declaration can be also written like
my_struct * create( char * );

Predefine a struct in C

I try to do this in C :
typedef struct s_match_fptr
{
char *str;
int (*funcptr)(t_client *client, char **command);
} t_match_fptr;
typedef struct s_client
{
int socket_fd;
int port;
char *server_ip;
struct sockaddr_in s_in;
t_match_fptr *db;
} t_client;
The point is I try to declare a function pointer that takes in parameter a t_client struct in my t_match_ptr struct.
Also, my struct t_client have an array of t_match_ptr.
For simplify, A need to be declared after B AND B needs to be declared after A.
So, is there a way to "predeclare" t_client before the declaration of t_match_ptr?
Thank you and sorry for bad english.
Forward declaration.
Add at the beginning: typedef struct s_client t_client;
Now the compiler will know the type t_client when encountered in s_match_fptr.
Note, the type must be used only by reference in the s_match_fptr definition (i.e. using a pointer). This way the compiler doesn't need to know the actual contents of the type when parsing the code.

how to understand the following code about typedef in C

Does anyone know how to understand the fourth line of the code shown below?
typedef short Signal;
typedef struct Event Event;
typedef struct Fsm Fsm;
typedef void (*State)(Fsm *, Event const *);
It declares State as a typedef for void (*)(Fsm *, Event const *).
void (*)(Fsm *, Event const *) is a function pointer, pointing to a function that takes two arguments, Fsm * and Event const *, and returns void.
More information: How do function pointers in C work? and Typedef function pointer?
Let's go through the typedefs one by one:
The first line creates an alias for the type short. Now you can write Signal xyz = 0; and it would be equivalent to writing short xyz = 0;
The second and third lines let you write declarations of variables of the two struct types without the struct keyword. In other words, you can now write Fsm myFsm; instead of writing struct Fsm myFsm;
The last line declares a type State that corresponds to a void function pointer taking a pointer to Fsm and a pointer to Event.
The syntax may be a little tricky because of all the parentheses and the name being typedef-ed not being at the end of the declaration. You can tell it's a type definition for a function pointer, because the name of the type is in parentheses, and is prefixed with an asterisk. The rest of the typedef looks very much like a function signature, so the result is easy to read.

Can somebody explain me this typedef from libusb?

I want to convert the libusb.h into PureBasic code and now I have this line:
typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
What does this typedef do?
Later on there are lines like this:
void LIBUSB_CALL libusb_free_device_list(libusb_device **list, int unref_devices);
Thanks in advance!
LIBUSB_CALL is just a type annotation. It probably doesn't do anything.
This declares a type "f" that is a function pointer returning void and taking "params":
typedef void (*f)(params...)
libusb_transfer_cb_fn is a type representing a function pointer tacking a pointer to a libusb_transfer struct and returning nothing (void).
The syntax for typedef in C is peculiar. What you do is write an ordinary declaration:
int x, *y, z[2];
and then insert the keyword typedef in front. This tells the compiler that, for each variable declared, don't actually declare a variable; instead, make that a new name for the type that the variable would have had, if it were a variable. So:
typedef int x, *y, z[2];
makes x an alias for int, y an alias for int *, and z an alias for int [2].
If you take the typedef off the original line you get:
void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
(which is only syntactically valid if LIBUSB_CALL is a macro, probably some compiler-specific modifier like __stdcall or __fastcall), which is already a little peculiar because of C's need to parenthesize pointers-to-functions. But it would declare (and define) libusb_transfer_cb_fn as a variable of type "pointer to function taking one argument (the transfer thing) and returning void. So the typedef makes libusb_transfer_cb_fn an alias for that type.
The name obviously (well... :-) ) means: "callback function for libusb after a transfer operation".
The idea would be that a later bit of C code might read something like:
extern libusb_transfer_cb_fn abc_func;
which tells you that there's some global variable abc_func of type "pointer to libusb callback", or:
some_return_type xyz_func(libusb_transfer_cb_fn funcp, int a, char *b) {
struct libusb_transfer x;
... some code that fills in x ...
(*funcp)(&x);
}

What does this typedef mean?

I am new to C, and this typedef looks a little bit strange to me. Can someone explain what it does?
typedef void (*alpm_cb_log)(alpm_loglevel_t, const char *, va_list);
It is in a header file.
You can use cdecl.org : http://cdecl.ridiculousfish.com/?q=void+%28*alpm_cb_log%29%28alpm_loglevel_t%2C+const+char+*%2C+va_list%29+
It says:
declare alpm_cb_log as pointer to function (alpm_loglevel_t, pointer to const char, va_list) returning void
in this case, it is a typedef, not a declaration.
A Simple example.
Declaration:
typedef int myint.
Use:
myint number = 7;
myint is a synonym of int.
your example
typedef void (*alpm_cb_log)(alpm_loglevel_t, const char *, va_list);
this is a pointer to a function
(*alpm_cb_log)
The arguments are
(alpm_loglevel_t, const char *, va_list)
and does not return anything.
void
The general rule with the use of typedef is to write out a declaration as if
you were declaring variables of the types that you want
It defines alpm_cb_log to be a type for a pointer to a function that takes the arguments alpm_loglevel_t, const char *, va_list and returns void.
These do look weird if you've never seen them before. It's a typedef alpm_cb_log for a pointer to a function returning void, taking two or more arguments: an alpm_loglevel_t, a const char *, and a variable argument list.
it creates the alais alpm_cb_log which is a pointer to a function that returns void and takes three paramaters. 1. a alpm_loglevel_t 2. const char *. 3 a varaibale argument list.

Resources