Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fundamental typedef operand syntax

Tags:

c++

c

typedef

Given:

typedef type-declaration synonym;

I can see how:

typedef long unsigned int size_t;

declares size_t as a synonym for long unsigned int, however I (know it does but) can't see exactly how:

typedef int (*F)(size_t, size_t);

declares F as a synonym for pointer to function (size_t, size_t) returning int

typedef's two operands (type-declaration, synonym) in the first example are long unsigned int and size_t.

What are the two arguments to typedef in the declaration of F or are there perhaps overloaded versions of typedef?

If there is a relevant distinction between C and C++ please elaborate otherwise I'm primarily interested in C++ if that helps.

like image 214
Peter McG Avatar asked Sep 23 '10 22:09

Peter McG


1 Answers

Type declarations using typedef are the same as corresponding variable declarations, just with typedef prepended. So,

        int x; // declares a variable named 'x' of type 'int'
typedef int x; // declares a type named 'x' that is 'int'

It's exactly the same with function pointer types:

        int(*F)(size_t); // declares a variable named F of type 'int(*)(size_t)'
typedef int(*F)(size_t); // declares a type named 'F' that is 'int(*)(size_t)'

It's not a "special case;" that's just what a function pointer type looks like.

like image 146
James McNellis Avatar answered Oct 12 '22 22:10

James McNellis