What type of typedef is this? - c

Here is the sample code
typedef int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];
What does the above code means?
INT is the alias for int. Rest of the code what is happening?

The one-line typedef in the question is a shortcut for
typedef int INT;
typedef int *INTPTR;
typedef int ONEDARR[10];
typedef int TWODARR[10][10];
Then INT is an alias for type int.
INTPTR is an alias for type int *.
ONEDARR is an alias for type int [10].
TWODARR is an alias for type int [10][10].
(https://en.cppreference.com/w/c/language/typedef)

Consider the following declaration
int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];
It declares four variables with type specifier int:
scalar variable INT
pointer *INTPTR
one-dimensional array ONEDARR[10]
and two-dimensional array TWODARR[10][10]
Then using the typedef
typedef int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];
then the names of variables mean not objects but aliases for types that had the variables if they were declared without the typedef.
So INT means the type int, INTPTR means the type int *, ONEDARR means the type int[10], and TWODARR means the type int[10][10].
So now you have a choice whether to declare an array the following way
int a'10][10];
or to specify its type using an alias for the type of the array
TWODARR a;
Consider one more example.
Let's assume you have a function declaration
int f( int x, int y );
It has the type int( int, int ). Now you want to name this type that instead of this long record int( int, int ) to use a shorter record. Then you can use a typedef like
typedef int FUNC( int x, int y );
and as result the name FUNC now denotes the type int( int, int ).

Related

typedef with function this is asked but couldnt understand?

HI i am unable to figure out how function pointer is typedef in below code .please can anyone explain
#include<stdio.h>
typedef int(*fp)(int,int) ; this is typedef with function pointer i m unable to figure out?
int sum (int,int);
int main()
{
fp p,q; // p, q become function pointer too how?
p=sum;
printf("%d\n",p(20,10));
}
int sum(int i,int j)
{
return(i+j);
}
Well, there's not much to understand. You define a new type int(*fp)(int, int). This is not a function pointer, but a function pointer type. Compare fp to float, int, or any other primitive types. Then you declare p and q of the above mentioned type and assign to p the function sum which is of the same type (implicit).
Function has signature
int NAME(int ,int );
so pointer to it is
int (*NAME_OF_POINTER)(int, int);
To predict long names define new type for function pointer
typedef int (*NEW_TYPE_NAME)(int, int);
And use it. So instead of
int (*myVar)(int, int) = sum;
You can write now
NEW_TYPE_NAME myVar = sum;

typedef int (*pf) needs explaining

Generally, we use typedef to get alternate names for datatypes.
For example --
typedef long int li; // li can be used now in place of long int
But, what does the below typedef do?
typedef int (*pf) (int, int);
typedef int (*pf) (int, int);
This means that variables declared with the pf type are pointers to a function which takes two int parameters and returns an int.
In other words, you can do something like this:
#include <stdio.h>
typedef int (*pf)(int,int);
int addUp (int a, int b) { return a + b; }
int main(void) {
pf xyzzy = addUp;
printf ("%d\n", xyzzy (19, 23));
return 0;
}
typedef long int li;
assigns alternate name li to type long int.
In exactly the same way
typedef int (*pf) (int, int);
assigns alternate name pf to type int (*) (int, int). That all there is to it.
As you probably noticed, typedef declarations follow the same syntax as, say, variable declarations. The only difference is that the new variable name is replaced by the new type name. So, in accordance with C declaration syntax, the declared name might appear "in the middle" of the declarator, when array or function types are involved.
For another example
typedef int A[10];
declares A as alternate name for type int [10]. In this example the new name also appears "in the middle" of the declaration.
It's a function pointer prototype. You can then declare a function as an argument something like this:
void RegisterCallback(pf your_callback_func);
Then you can can call the function passed as a func ptr:
...
your_callback_func(i, j);
...
The typedef has the name pf and it is for a function pointer that takes two integers as arguments and returns an integer.
typedef works as:
Define unknown type with known types.
So it defines function type that takes two int argument and return int.

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);
}

Pointer to function as parameter, using typedef

I'm trying to create code which uses pointers to functions as parameteres, and I have to use a typedef.
I'm not a C pro. It feels like I'm getting there, but I can't seem to find a good explanation of the syntax of pointers to function.
I have a function fillArray:
long fillArray(long *array, int x, int y) {
//
}
then I want to make a typedef of a pointer to this function:
typedef long (*fArray)(long, int, int);
fArray pFill = fillArray;
and I want to give this pFill to a function called doThis():
int doThis (fArray pFill) {
return 0;
}
and calling it using:
int y = doThis(pFill);
What am I doing wrong?
Your typedef needs to be:
typedef long (*fArray)(long *, int, int);
^
Your fillArray function accepts a long * as first parameter, you forgot a * into the typedef:
typedef long (*fArray)(long *, int, int);

What's the difference between these 2 declarations in c?

typedef int (*P)(char *(*)());
int (*P)(char *(*)());
Both seems to be doing the same thing to me,what's the typedef there for?
The first declares a type called P that you can use in the declaration of other variables. The second declares a variable of that same type.
For illustrative purposes:
typedef int (*P)(char *(*)());
int main() {
int (*Q)(char *(*)());
P R;
}
In this example the variables Q and R have exactly the same type.
The typedef defines P to be a function pointer type. The second version defines P to be a function pointer.
A type can be used to declare variables. After
typedef int (*P)(char *(*)());
you can use
P p;
which will be equivalent to
int (*p)(char *(*)());
The simple answer would be you are creating a new datatype through typedef.
Let's take a simple example, in embedded system we use only unsigned numbers.
Now 1 way is I write
unsigned int xyz;
So here I would have to type unsigned everywhere.. What if I forget to type unsigned somewhere, it's very difficult to figure out that if the code is released. So simple way would be
typedef unsigned int uint;
So now you can use uint as a datatype. So whenever parser encounter uint, it would read it as unsigned int.
So in your case you can use P as a datatype in code. So Like in the first example
P xyz ;
would be parsed as
int (*xyz)(char *(*)());

Resources