Typedefs for complex data types - c

I am attempting to understand the underlying mechanics of how C handles complex typedefs, from a syntax perspective.
Consider the following examples below (references included at end of the question).
typedef int (*p1d)[10];
is the proper declaration, i.e. p1d here is a pointer to an array of
10 integers just as it was under the declaration using the Array type.
Note that this is different from
typedef int *p1d[10];
which would make p1d the name of an array of 10 pointers to type int.
So, if I consider operator precedence for both examples (I'll rewrite them):
int *p1d[10]; // Becomes ...
int* p1d[10];
So, reading left-to-right, and using operator precedence, I get: "Pointer to type int, named p1d, of size 10", which is wrong. As for the other/first case:
int (*p1d)[10];
Which I read as "p1d is a pointer, of type int, and is an array of 10 such elements", which is also wrong.
Could someone explain the rules applied for determining these typedefs? I would like to apply them to function pointers as well, and I'm hoping this discussion will also explain the logic behind const casts (ie: pointer to constant data vs const pointer to variable data).
Thank you.
References:
C Tutorial: Pointers to Arrays: http://www.taranets.net/cgi/ts/1.37/ts.ws.pl?w=329;b=285
Operator Precedence: http://www.swansontec.com/sopc.html

One of my professors wrote this little guide to reading these kinds of declarations. Give it a read, it'll be worth your while and hopefully answer any questions.
All credit goes to Rick Ord (http://cseweb.ucsd.edu/~ricko/)
The "right-left" rule is a completely regular rule for deciphering C
declarations. It can also be useful in creating them.
First, symbols. Read
* as "pointer to" - always on the left side
[] as "array of" - always on the right side
() as "function returning" - always on the right side
as you encounter them in the declaration.
STEP 1
------
Find the identifier. This is your starting point. Then say to yourself,
"identifier is." You've started your declaration.
STEP 2
------
Look at the symbols on the right of the identifier. If, say, you find "()"
there, then you know that this is the declaration for a function. So you
would then have "identifier is function returning". Or if you found a
"[]" there, you would say "identifier is array of". Continue right until
you run out of symbols *OR* hit a *right* parenthesis ")". (If you hit a
left parenthesis, that's the beginning of a () symbol, even if there
is stuff in between the parentheses. More on that below.)
STEP 3
------
Look at the symbols to the left of the identifier. If it is not one of our
symbols above (say, something like "int"), just say it. Otherwise, translate
it into English using that table above. Keep going left until you run out of
symbols *OR* hit a *left* parenthesis "(".
Now repeat steps 2 and 3 until you've formed your declaration. Here are some
examples:
int *p[];
1) Find identifier. int *p[];
^
"p is"
2) Move right until out of symbols or right parenthesis hit.
int *p[];
^^
"p is array of"
3) Can't move right anymore (out of symbols), so move left and find:
int *p[];
^
"p is array of pointer to"
4) Keep going left and find:
int *p[];
^^^
"p is array of pointer to int".
(or "p is an array where each element is of type pointer to int")
Another example:
int *(*func())();
1) Find the identifier. int *(*func())();
^^^^
"func is"
2) Move right. int *(*func())();
^^
"func is function returning"
3) Can't move right anymore because of the right parenthesis, so move left.
int *(*func())();
^
"func is function returning pointer to"
4) Can't move left anymore because of the left parenthesis, so keep going
right. int *(*func())();
^^
"func is function returning pointer to function returning"
5) Can't move right anymore because we're out of symbols, so go left.
int *(*func())();
^
"func is function returning pointer to function returning pointer to"
6) And finally, keep going left, because there's nothing left on the right.
int *(*func())();
^^^
"func is function returning pointer to function returning pointer to int".
As you can see, this rule can be quite useful. You can also use it to
sanity check yourself while you are creating declarations, and to give
you a hint about where to put the next symbol and whether parentheses
are required.
Some declarations look much more complicated than they are due to array
sizes and argument lists in prototype form. If you see "[3]", that's
read as "array (size 3) of...". If you see "(char *,int)" that's read
as "function expecting (char *,int) and returning...". Here's a fun
one:
int (*(*fun_one)(char *,double))[9][20];
I won't go through each of the steps to decipher this one.
Ok. It's:
"fun_one is pointer to function expecting (char *,double) and
returning pointer to array (size 9) of array (size 20) of int."
As you can see, it's not as complicated if you get rid of the array sizes
and argument lists:
int (*(*fun_one)())[][];
You can decipher it that way, and then put in the array sizes and argument
lists later.
Some final words:
It is quite possible to make illegal declarations using this rule,
so some knowledge of what's legal in C is necessary. For instance,
if the above had been:
int *((*fun_one)())[][];
it would have been "fun_one is pointer to function returning array of array of
^^^^^^^^^^^^^^^^^^^^^^^^
pointer to int". Since a function cannot return an array, but only a
pointer to an array, that declaration is illegal.
Illegal combinations include:
[]() - cannot have an array of functions
()() - cannot have a function that returns a function
()[] - cannot have a function that returns an array
In all the above cases, you would need a set of parens to bind a *
symbol on the left between these () and [] right-side symbols in order
for the declaration to be legal.
Here are some legal and illegal examples:
int i; an int
int *p; an int pointer (ptr to an int)
int a[]; an array of ints
int f(); a function returning an int
int **pp; a pointer to an int pointer (ptr to a ptr to an int)
int (*pa)[]; a pointer to an array of ints
int (*pf)(); a pointer to a function returning an int
int *ap[]; an array of int pointers (array of ptrs to ints)
int aa[][]; an array of arrays of ints
int af[](); an array of functions returning an int (ILLEGAL)
int *fp(); a function returning an int pointer
int fa()[]; a function returning an array of ints (ILLEGAL)
int ff()(); a function returning a function returning an int
(ILLEGAL)
int ***ppp; a pointer to a pointer to an int pointer
int (**ppa)[]; a pointer to a pointer to an array of ints
int (**ppf)(); a pointer to a pointer to a function returning an int
int *(*pap)[]; a pointer to an array of int pointers
int (*paa)[][]; a pointer to an array of arrays of ints
int (*paf)[](); a pointer to a an array of functions returning an int
(ILLEGAL)
int *(*pfp)(); a pointer to a function returning an int pointer
int (*pfa)()[]; a pointer to a function returning an array of ints
(ILLEGAL)
int (*pff)()(); a pointer to a function returning a function
returning an int (ILLEGAL)
int **app[]; an array of pointers to int pointers
int (*apa[])[]; an array of pointers to arrays of ints
int (*apf[])(); an array of pointers to functions returning an int
int *aap[][]; an array of arrays of int pointers
int aaa[][][]; an array of arrays of arrays of ints
int aaf[][](); an array of arrays of functions returning an int
(ILLEGAL)
int *afp[](); an array of functions returning int pointers (ILLEGAL)
int afa[]()[]; an array of functions returning an array of ints
(ILLEGAL)
int aff[]()(); an array of functions returning functions
returning an int (ILLEGAL)
int **fpp(); a function returning a pointer to an int pointer
int (*fpa())[]; a function returning a pointer to an array of ints
int (*fpf())(); a function returning a pointer to a function
returning an int
int *fap()[]; a function returning an array of int pointers (ILLEGAL)
int faa()[][]; a function returning an array of arrays of ints
(ILLEGAL)
int faf()[](); a function returning an array of functions
returning an int (ILLEGAL)
int *ffp()(); a function returning a function
returning an int pointer (ILLEGAL)

Simplfying KepaniHaole's rules a bit, it boils down to:
Find the left-most identifer
Work your way out, remembering that absent explicit grouping by parentheses, function-call () and [] bind before *.
Apply recursively to any function parameters.
Thus, T *a[] is an array of pointer tp T, T (*a)[] is a pointer to an array of T, T *f() is a function returning a pointer to T, , and T (*f)() is a pointer to a function returning T.
Taking an example that gives a lot of people heartburn, we can look at the prototype for the POSIX signal function:
void (*signal( int sig, void (*func)( int )))( int );
which reads as
signal -- signal
signal( ) -- is a function with
signal( sig ) -- parameter sig
signal( int sig ) -- of type int
signal( int sig, func ) -- and parameter func
signal( int sig, (*func) ) -- of type pointer to
signal( int sig, (*func)( )) -- a function with
signal( int sig, (*func)( int )) -- an int parameter
signal( int sig, void (*func)( int )) -- returning void
(*signal( int sig, void (*func)( int ))) -- returning a pointer to
(*signal( int sig, void (*func)( int )))( ) -- a function with
(*signal( int sig, void (*func)( int )))( int ) -- an int parameter
void (*signal( int sig, void (*func)( int )))( int ) -- returning void

Related

pass the location of a an array in a function

How can I pass the location of an array of pointers in a function in c i.e.
I have an int* array[10]; and through a function I want to assign the array[0] to be the location of a particular number say, 5. What should the argument of the function look like. I have tried:
void insert(int* array);
but it didn't work.
Arrays are always passed as a pointer pointing to the beginning of the array, even if you don't write it explicitly. So given this declaration of an array from your question:
int *array[10];
Both the following functions will accept a pointer to such an array:
int foo(int *array[]);
int foo(int **array);
They are just equivalent and I would suggest you use the second form, because it makes it explicit what type really is passed to the function.
inside the function, you can access any element, because the indexers are define the same way on a pointer to an array as on the array itself:
int foo(int **array)
{
int *sixthElement = array[5];
// [...]
}
Additional stylistic note: In C, it's common practice to attach the * when declaring a pointer to the identifier, not to the type, ie better write
int *array[10];
instead of
int* array[10];
This is because in a C declaration, there's no such thing as a pointer type, there's a pointer declarator (consisting of the *, optional qualifiers and the identifier) and this declarator declares a variable to be a pointer to the specified type. It's a subtle difference, but if you ever try to declare more than one variable in one line (don't do this, it's just for explanation here), you will notice it:
int *a, b; // <- declares a as a pointer to int, b just as an int

C function signature with asterisk

I have been attempting C recently and have been reading C source code,
however I came across this perculiar method signature with a * beside its name,
can someone explain this to me please
int *bubble_sort(int *numbers, int count, compare_cb cmp)
edit: I am referring to the * infront of bubble_sort
The asterisk has nothing to do with the function signature, it's just C's way of declaring pointers. The asterisk belongs to the type on the left, i.e. the return type for the function is int *, which is read out as "pointer to integer". The first argument has the exact same type, pointer to integer. You use this syntax everywhere in C, not just with function declarations.
The '*', used in this way, indicates that the 'bubble_sort()' function returns an address of (in other words, 'a pointer to) an integer, rather than an integer.
This concept is of pointers.
To know more about pointers see this link : http://www.tutorialspoint.com/cprogramming/c_pointers.htm
int *bubble_sort(int *numbers, int count, compare_cb cmp)
int *bubble_sort means that you are going to return the address of the integer,
and int *numbers means that you are getting the address of the variable as argument.
int a() is a function returning an int.
int * a() is a function returning a pointer to an int.
in your case the function returns a pointer to an integer.
guessing from the function name it is returning the sorted array.
an array in C is notated as a pointer to the first element:
int a[5];
int *b=a;
a and b are the same
However I guess you still have to do some more research on pointers which is not as complicated.
Perhaps this links might help: Pointer Primer

Is it possible to assign a new value to an array name?

I read that;
Although an array name can be used as a pointer (after decaying into pointer), it's not possible to assign it a new value. Attempting to make it point elsewhere is an error:
while (*a != 0) // a is of (int *) type
a++; //wrong
On the other hand, when passed to a function, an array name is always treated as a pointer. The function call
largest = largest_num(b, n) // b is an array of int
for the function
int find_largest(int a[], int n)
{
....
....
}
causes a pointer to the first element of b to be assigned to a.
Above two statements ( in bold ) seems to me contradictory. I am confused.
In a function declaration, an array is treated as if you'd declared a pointer, so
int find_largest(int a[], int n) {
is processed as if it were
int find_largest(int *a, int n) {
So a is a pointer, not an array, and there's no contradiction.
Since it's a pointer, you can reassign a, e.g.
a++;
is allowed.
No contradiction there - you're still working with a pointer (to int), and int a[] notation is allowed only for convenience. Quoting the comp.lang.c FAQ:
Since arrays decay immediately into pointers, an array is never
actually passed to a function. You can pretend that a function
receives an array as a parameter, and illustrate it by declaring the
corresponding parameter as an array:
void f(char a[])
Interpreted literally, this declaration would have no use, so the
compiler turns around and pretends that you'd written a pointer
declaration, since that's what the function will in fact receive:
void f(char *a)
This conversion of array-like declarators into pointers holds
only within function formal parameter declarations, nowhere else. If
the conversion bothers you, you're under no compulsion to make use of
it; many programmers have concluded that the confusion it causes
outweighs the small advantage of having the declaration ``look like''
the call or the uses within the function.

What type is q in "void foo(int q[][4]){}" ?How is using "void foo(int q[6][4]){}" different from the former?

I am certain q in int q[6][4] is of type (**q)[4], ie, pointer to a pointer to an integer array sized 4. But the book I have (I find it dubious!!) says that the int q[][4] part in the function definition void foo(int q[][4]){} is the same as int (*q)[4].I am ambivalent about the book, yet let me present for your consideration some issues that crop up in my mind over this.Your detailed explanation is very much sought.
1) During declaration,is the type of q in int q[][4] the same as in int q[6][4]? Contrary to what the book says, I see q[][4] as nothing but q[0][4] and I feel q is of type (**q)[4],and not (*q)[4].Am I right?What's your take on it?
2) (Most confusing bit) I know passing int *q and int q[] (or int q[4]) is the same in C as the latter reduces to the former.But I have verified from the compiler that int (*q)[5] is not the same type as int (*q)[4], so what is the difference between passing as arguments to a function A) int (*q)[] B)int (*q)[4] and C)int (*q)[5] ? Please be detailed for the answer to this part.
3) How is passing int q[][4] to a function different from passing int q[6][4] in C?Does q reduce to (**q)[4] in both cases?
I am certain q in int q[6][4] is of type (**q)[4], ie, pointer to a pointer to an integer array sized 4
No. (The pointer-to-pointer-to-array is very, very far from the truth, specifically.) q is of type int[6][4], i. e. an array of 6 arrays of 4 integers.
When passed to a function, it's only the first (innermost) dimension of an array that decays into a pointer. So, int[6][4] decays into int (*)[4], and so does int[][4].
int (*q)[5] is not the same type as int (*q)[4], so what is the difference between passing as arguments to a function A) int (*q)[] B) int (*q)[4] and C) int (*q)[5]
A) is an incomplete type (pointer to array of any size). You can't dereference it, nor can you perform pointer arithmetic on it.
B) is a pointer to array of 4 ints. C) is a pointer to array of 5 ints.
How is passing int q[][4] to a function different from passing int q[6][4] in C?
Semantically, they mean the same. They both decay into int (*)[4].
Does q reduce to (**q)[4] in both cases?
No, but I've already explained that.
For the first question, q is an array of array of four integers.
You might want to read about the Clockwise/Spiral Rule. It might actually help you with the second question.
1) During declaration,is the type of q in int q[][4] the same as in
int q[6][4]?
Yes. The first dimension is not needed in a function declaration and a multidimensional array in a function declaration has type pointer to an array.
what is the difference between passing as arguments to a function A)
int (*q)[] B)int (*q)[4] and C)int (*q)[5] ?
int (*q)[] is a pointer to an array of unknown size. int (*q)[4] is a pointer to int[4] and int (*q)[5] is a pointer to int[5]. They all have different types.
3) How is passing int q[][4] to a function different from passing int
q[6][4] in C?Does q reduce to (**q)[4] in both cases?
They are both equivalent to int (*q)[4].
Pleas read 23.1: Multidimensional Arrays and Functions of the c.faq for more details.

Complex declarations

How do I interpret complex declarations like:
int * (* (*fp1) (int) ) [10]; ---> declaration 1
int *( *( *[5])())(); --------> declaration 2
Is there any rule that should be followed to understand the above declarations?
Here is a great article about how to read complex declarations in C: http://www.codeproject.com/KB/cpp/complex_declarations.aspx
It helped me a lot!
Especially - You should read "The right rule" section. Here quote:
int * (* (*fp1) (int) ) [10];
This can be interpreted as follows:
Start from the variable name -------------------------- fp1
Nothing to right but ) so go left to find * -------------- is a pointer
Jump out of parentheses and encounter (int) --------- to a
function that takes an int as argument
Go left, find * ---------------------------------------- and returns a pointer
Jump put of parentheses, go right and hit [10] -------- to an array of
10
Go left find * ----------------------------------------- pointers to
Go left again, find int -------------------------------- ints.
You can use cdecl*:
cdecl> explain int *( *( *a[5])())();
declare a as array 5 of pointer to function
returning pointer to function returning pointer to int
cdecl> explain int * (* (*fp1) (int) ) [10];
declare fp1 as pointer to function (int) returning
pointer to array 10 of pointer to int
*Linked is a website that uses this command line tool in the backend.
I've learned the following method long ago:
Start from the type identifier (or the inner parenthesis) and move following a spiral taking the element at right first
In case of
int * (* (*fp1) (int) ) [10];
You can say:
fp1 is a (nothing on the right so move left)
pointer to (move out of the inner parenthesis
a function taking int as agument (the 1st on the right)
and returns a pointer to (exit from parenthesis)
an array of 10 elements of type
pointer to (nothing left on the right)
int
Resulting in:
fp1 is a pointer to a function taking an int and returning a pointer to an array of 10 pointers to int
Drawing the actual spiral (in you your mind, at least) helps a lot.
For solving these complicated declarations, the rule you need to keep in mind is that the precedence of function-call operator () and array subscript operator [] is higher than dereference operator *. Obviously, parenthesis ( ) can be used to override these precedences.
Now, work out your declaration from the middle, which means from the identifier name.
int * (* (*fp1) (int) ) [10]; --->declaration 1
Based on the precedences rule mentioned above, you can easily understand it by breaking down the declaration as
fp1 * (int) * [10] * int
and read it directly from left-to-right in English as
"fp1 is a pointer to a function accepting an int & returning a pointer to an array [10] of pointers to int". Note that the declaration is broken this way only to help understand it manually. The compiler need NOT parse it this way.
Similarly,
int *( *( *[5])())(); -------->declaration 2
is broken as
[5] * () * () * int
So, it declares "an array [5] of type pointers to function () which returns a pointer to a function () which in turn returns a pointer to int".
Though it's has been answered already, but you may also read this article :
http://unixwiz.net/techtips/reading-cdecl.html
Start with the leftmost identifier and work your way out, remembering that absent any explicit grouping [] and () bind before *, e.g:
*a[] -- is an array of pointer
(*a)[] -- is a pointer to an array
*f() -- is a function returning pointer
(*f)() -- is a pointer to a function
Thus, we read int *(*(*fp1)(int))[10] as:
fp1 -- fp1
*fp1 -- is a pointer
(*fp1)(int) -- to a function
taking an int parameter
*(*fp1)(int) -- returning a pointer
(*(*fp1)(int))[10] -- to a 10-element array
*(*(*fp1)(int))[10] -- of pointer
int *(*(*fp1)(int))[10] -- to int
The declaration int *(*(*[5])())() presents a bit of a challenge since there's no identifier; you typically see this in function declarations where a parameter is of that type:
void foo(int *(*(*[5])())(), double);
It's the same principle as the unnamed int parameter in the declaration of fp1. The array gives us the clue, you can also look for the leftmost inner grouping of parentheses.
-- unnamed
[5] -- is a 5-element array ([] binds before *)
*[5] -- of pointers
(*[5])() -- to functions
*(*[5])() -- returning pointers
(*(*[5])())() -- to functions
*(*(*[5])())() -- returning pointers
int *(*(*[5])())() -- to int
The clockwise/spiral:
* http://c-faq.com/decl/spiral.anderson.html
No, you don't need to read it loud with complex steps like "clockwise/spiral rule" and "the right rule". Why should you? You only need to know how to use it! Don't make the simple one complex.
C declarations in fact work in a simple rule: declare as how would be used.
Consider the code you gives out:
int * (* (*fp1) (int) ) [10]; ---> declaration 1
int *( *( *[5])())(); --------> declaration 2
For the first declaration, this means that *(*(*fp1)(int))[int] is an int. And that's it.
For example, you know that *(*(*fp1)(5))[0] is an int, and *(*(*fp1)(2))[9] is an int too.
And The second declaration is incomplete. Even gcc won't know what you want to convey.

Resources