Reputation: 3887
While messing around with the type syntax, I noticed this is legal :
typedef int *((* T)[10]);
T fun(){
return 0;
};
int main(int argc, char * argv[]){
//int c = fun(); // (1)
return 0;
}
...And if you uncomment (1)
, then you get an error message of this kind (GCC / Clang) : "error: cannot initialize a variable of type 'int' with an rvalue of type 'T' (aka 'int *((*)[10])')
" (Normal so far). Notice however the "aka
" that points out the type is an alias of int *((*)[10])
and not simply int ***
However, It seems impossible to declare a function with this type without using a typedef :
int *((*)[10]) fun(){ // The compiler does not approve
return 0;
};
int *((* fun2)[10]) (){ // The compiler does not approve either
return 0;
};
int main(int argc, char * argv[]){
//int c = fun(); // (1)
return 0;
}
...Then I was wondering why ? (the question is for the C language, but it looks like it's the same for C++)
Upvotes: 2
Views: 82
Reputation:
The original
typedef int *((* T)[10])
can shed the outer parens:
typedef int *(* T)[10]
Or aligned with dbush's function:
typedef int *(* T )[10]
int *(* fun() )[10]
Upvotes: 0
Reputation: 224072
This type:
typedef int *((* T)[10]);
Is a pointer to an array of size 10 whose members are of type int *
. This is not the same as an int ***
.
As for creating a function that returns this type, you would need this:
int *(*fun())[10] {
return 0;
};
But using a typedef
makes this much clearer.
Upvotes: 3
Reputation: 63124
int *((*fun())[10]) {
return 0;
};
... Yup. You should probably stick to the typedef for the sake of readability :)
Upvotes: 2