Originally Posted by
Mr.Lnx
Hello to all. I have some complex declarations to simplify with typedef I have done a try
1.
Code:
char (*x[10]) (int);
/* typedef char FUNC(int);
typedef FUNC *FUNC_PTR;
FUNC_PTR x[10]; */
Why we don't use * symbol in the last statement in front of FUNC_PTR?
Because you can't have a * as the left-most symbol in a declaration.
The way you were talking about it would expand to something like:
*char (*x[10]) (int);
Which is not valid C syntax
The * symbol is embedded in the definition of FUNC_PTR (which is a FUNC *). Note, this has something to do with operator precedence. The [10] is processed before the *. Expand it one layer at a time
x is an array of 10 FUNC_PTRs
x is an array of 10 (pointer to FUNC or FUNC *) -- there is your *
x is an array of 10 (pointer to (function taking int and returning char))
Originally Posted by
Mr.Lnx
3.
Code:
float *(*x(void))(int);
/* typedef float *FUNC(int);
FUNC *x(void); */
Those appear to be the same. What makes you think they aren't? If they truly are the same, then you can redeclare x as both types (the typedef'ed and non-typedef'ed) and the compiler shouldn't care. When I compile the following program, I get no errors:
Code:
$ cat foo.c
typedef float *FUNC(int);
int main(void)
{
float *(*x(void))(int);
FUNC *x(void);
return 0;
}
$ make foo
gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
$
But if I make a small change to FUNC, so it returns float instead of float *, I get the following error:
Code:
$ cat foo.c
typedef float FUNC(int);
int main(void)
{
float *(*x(void))(int);
FUNC *x(void);
return 0;
}
$ make foo
gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
foo.c: In function ‘main’:
foo.c:6:11: error: conflicting types for ‘x’
foo.c:5:14: note: previous declaration of ‘x’ was here
make: *** [foo] Error 1
EDIT: I don't know if you actually had a question about your number 2.
If it's giving you problems, you may be missing a semicolon at the end (or you just didn't copy it)