typedef-不带花括号的Typedef

如何解决typedef-不带花括号的Typedef

我是C语言的新手,但是这种语法是什么意思?

typedef Value (*NativeFn)(int argCount,Value* args);

据我了解,此处使用“值”来定义新名称的类型。 我不了解的部分是(*NativeFn)(int argCount,Value* args);,这部分是什么意思?

解决方法

其他人正确地说了这一点:

typedef Value (*NativeFn)(int argCount,Value* args);

为函数指针类型创建typedef名称NativeFn

typedef的语法与一般的C声明的语法一样,可能会造成混淆。 typedef功能实际上是在建立声明语法后添加到语言中的,并且必须在不破坏其他任何内容的情况下进行添加。解决方案是在语法上将typedef视为存储类说明符(尽管在语义上不是一个)。 typedef以外的存储类指定符是externstatic_Thread_localautoregister

这意味着您可以通过将关键字typedef替换为typedef来理解static声明。如果static声明声明某种类型的对象(或函数),则相应的typedef声明将创建具有相同名称和类型的类型定义。所以这个:

static int foo;

创建类型为foo(具有静态存储持续时间)的对象int

typedef int foo;

创建类型名称foo,它是类型int的别名。

因此,如果您的声明是:

static Value (*NativeFn)(int argCount,Value* args);

它将已将NativeFn定义为指向函数的对象(函数返回类型为Value的结果)。用static替换typedef意味着NativeFn是一个类型名称,它引用相同的指向函数的指针类型。

记住typedef不会创建新类型也很重要。它会为现有类型创建一个新名称。

,

考虑这样的记录

Value (int argCount,Value* args)

它表示具有返回类型Value和两个参数intValue *的函数类型。

标识符Value在其他地方声明,例如可以是类型的别名。

要为您可以编写的函数类型引入指针类型的别名

typedef Value (*NativeFn)(int argCount,Value* args);

因此,如果您具有例如这样的功能

Value some_function(int argCount,Value* args);

然后您可以通过以下方式使用typedef别名定义声明此函数的指针

NativeFn pointer_to_some_function = some_function; 
,

好吧

typedef (*NativeFn)(int argCount,Value* args)

表示

NativeFn是一个function type(can also be called "a pointer to function" or "function pointer"),它返回Value,并以intValue *作为参数。

如果您很难理解为什么以及如何使用它,那么请阅读下面的代码,特别是注释,您会清楚(*NativeFn)(int argCount,Value* args)的含义和用法:>

#include <stdio.h>

// note: "Value" is a type declared earlier
//       for discussions sake we're declaring our own Value type

typedef struct __value {
    int x,y;
} Value;


// now,the following tells us that:
// "NativeFn" is some function type that returns "Value"
typedef Value (*NativeFn)(int argCount,Value* args);


// okay,see how to use "NativeFn"
// for use "NativeFn" we've to declare some function first
// which takes two arguments same as "NativeFn" and return "Value"
Value someFun(int argCount,Value* args) {
    // do something argCount and args
    // at last it should return some "Value" type
    Value v = {2,3};
    return v;
}

int main() {
    // now its time to use "NativeFn"
    NativeFn fun;

    fun = someFun; // notice we can use fun as a variable and assign a
    // function to it,which must take arguments same as "NativeFn" and returns "Value" type

    Value input = {10,12};
    
    Value output = fun(1,&input); // note,we're calling "fun",not "someFun"

    // it'll output 2,3,cause we're returning Value v = {2,3} from "someFun"
    printf("(x,y): %d,%d\n",output.x,output.y);
    
    return 0;
}

如果您有任何问题,请在评论中问我...

,

typedef工具用于为类型创建别名。例如,

typedef int *iptr;

创建名称iptr作为类型int *的同义词,因此您可以使用来声明指针

iptr p,q;

代替写作

int *p,*q;

C声明语法比大多数人意识到的要复杂一些,尤其是在涉及指针的地方。基本规则是:

T *p;            // p is a pointer to T
T *a[N];         // a is an array of pointer to T
T *f();          // f is a function returning pointer to T
T (*a)[N];       // a is a pointer to an array of T
T (*f)();        // f is a pointer to a function returning T
T const *p;      // p is a pointer to const T
const T *p;      // same as above
T * const p;     // p is a const pointer to T

事情可能会变得非常复杂-您可以拥有指向函数的指针数组:

T (*a[N])();

或返回数组指针的函数:

T (*f())[N];
甚至更残暴的暴行。当混合中有参数时,函数指针的声明会更难看;幸运的是,您只需要在声明中列出参数 types 而不是名称:
typedef Value (*NativeFn)(int,Value*);

使事情变得更简单。

该声明创建NativeFn作为类型“要使用intValue *并返回Value的函数指针”的同义词。

假设您将函数定义为

Value foo( int argcCount,Value *args )
{
  Value result;
  ...
  return result;
}

如果要创建一个指向此函数的指针,名为fptr,通常将其声明为

Value (*fptr)(int,Value *) = foo;

但是,上面的typedef声明允许您编写

NativeFn fptr = foo;

话虽如此,请typedef 保留使用。问题在于,尽管它可以创建易于阅读的声明某些项目的方式,但它也隐藏了一些潜在有用的信息。例如,尽管上面的iptr示例,最好不要将指针隐藏在typedef后面-如果有人需要在*或{上使用一元p运算符{1}}为了正确使用它们,则该信息需要包含在这些项目的声明中。如果有人需要调用q所指的东西,那么他们需要知道返回类型是什么,参数的数量和类型等,但是所有这些信息都将丢失在使用typedef名称的声明中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res