• typedef


     1 int inc(int a)
     2 {
     3  return(++a);
     4 }
     5 int multi(int*a,int*b,int*c)
     6 {
     7  return(*c=*a**b);
     8 }
     9 typedef int(FUNC1)(int in);
    10 typedef int(FUNC2) (int*,int*,int*);
    11  
    12 void show(FUNC2 fun,int arg1, int*arg2)
    13 {
    14  FUNC1 * p=&inc;
    15  int temp =p(arg1);
    16  fun(&temp,&arg1, arg2);
    17  printf("%d
    ",*arg2);
    18 }
    19  
    20 main( )
    21 {
    22  int a;
    23  show(multi,10,&a);
    24  return 0;
    25 }
    View Code
     
     1 typedef int(FUNC1)(int in); 
     2 定义了一个函数类型 FUNC1,该函数类型参数是int in,返回值是int型!
     3 
     4 #include<stdio.h>
     5 int inc(int a)
     6 {
     7  return(++a); //计算a+1
     8 } 
     9 int multi(int*a,int*b,int*c)
    10 {
    11  return(*c=*a**b); //返回a×b 
    12 } 
    13 typedef int(FUNC1)(int in); //定义函数
    14 typedef int(FUNC2) (int*,int*,int*); 
    15 
    16 void show(FUNC2 fun,int arg1, int*arg2)
    17 {
    18  FUNC1* p=&inc; //这里应该是你复制的有错误,定义函数指针p,指向inc。
    19  int temp =p(arg1); //arg1=10 所以temp=10+1=11
    20  fun(&temp,&arg1, arg2); //fun就是multi函数,计算*arg2=11*10=110
    21  printf("%d
    ",*arg2);//这里输出110
    22 } 
    23 main()
    24 { 
    25  int a;
    26  show(multi,10,&a);
    27  return 0;
    28 }
    typedef code eg

    typedef与using有什么区别?

    using 是C++11用来扩展typedef 的, 不在typedef上扩展是为了尽可能保持C语言的兼容性。

    定义模板的别名,只能使用using

     have a series of functions with the same prototype, say

    int func1(int a, int b) {
      // ...
    }
    int func2(int a, int b) {
      // ...
    }
    // ...

    Now, I want to simplify their definition and declaration. Of course I could use a macro like that:

    #define SP_FUNC(name) int name(int a, int b)

    But I'd like to keep it in C, so I tried to use the storage specifier typedef for this:

    typedef int SpFunc(int a, int b);

    This seems to work fine for the declaration:

    SpFunc func1; // compiles

    but not for the definition:

    SpFunc func1 {
      // ...
    }

    which gives me the following error:

    error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

    Is there a way to do this correctly or is it impossible? To my understanding of C this should work, but it doesn't. Why?


    Note, gcc understands what I am trying to do, because, if I write

    SpFunc func1 = { /* ... */ }

    it tells me

    error: function 'func1' is initialized like a variable

    Which means that gcc understands that SpFunc is a function type.

    You cannot define a function using a typedef for a function type. It's explicitly forbidden - refer to 6.9.1/2 and the associated footnote:

    The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition.

    The intent is that the type category in a function definition cannot be inherited from a typedef:

    typedef int F(void); // type F is "function with no parameters
                         // returning int"
    F f, g; // f and g both have type compatible with F
    F f { /* ... */ } // WRONG: syntax/constraint error
    F g() { /* ... */ } // WRONG: declares that g returns a function
    int f(void) { /* ... */ } // RIGHT: f has type compatible with F
    int g() { /* ... */ } // RIGHT: g has type compatible with F
    F *e(void) { /* ... */ } // e returns a pointer to a function
    F *((e))(void) { /* ... */ } // same: parentheses irrelevant
    int (*fp)(void); // fp points to a function that has type F
    F *Fp; //Fp points to a function that has type F
  • 相关阅读:
    what are Datatypes in SQLite supporting android
    Version of SQLite used in Android?
    使用(Drawable)资源———ShapeDrawable资源
    使用(Drawable)资源——LayerDrawable资源
    使用(Drawable)资源——StateListDrawable资源
    使用(Drawable)资源——图片资源
    数组(Array)资源
    使用字符串、颜色、尺寸资源
    资源的类型及存储方式——使用资源
    资源的类型及存储方式——资源的类型以及存储方式
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/5793781.html
Copyright © 2020-2023  润新知