6.087 Practical Programming in C, lec8

本文详细探讨了C语言中的Void指针和Function指针的概念、用途及应用场景,包括Void指针如何用于指向任意数据类型,以及Function指针如何用于传递函数作为参数。同时介绍了回调函数和哈希表的基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Void and function pointers. Hash tables.

Void pointers

• C does not allow us to declare and use void variables.

• void can be used only as return type or parameter of afunction.

• C allows void pointers

• Question: What are some scenarios where you want to pass voidpointers?

• void pointers can be used to point to any data type

• int x; void∗ p=&x;/∗points to int ∗/

• float f ;void∗ p=&f;/∗points to float ∗/

• void pointers cannot be dereferenced. The pointers shouldalways be cast before dereferencing.

void∗ p; printf ("%d",∗p);/∗ invalid ∗/

void∗ p; int ∗px=(int∗)p; printf ("%d",∗px); /∗valid ∗/

C中的void像数学中的“0”一样,看似很简单,但其出现具有重大意义。变量之所以不能声明为void,我认为是因为变量一声明就在内存中分配空间了,既然分配了空间,那么就不能叫void了。但变量有时候需要表示空,这时只好用NULL来表示,NULL是宏,其数值一般是0

无论什么类型的指针,其都具有一个“父类型”,指针。因此void指针也是指针,指向的数据类型为“空”而已。“万物皆空”,因此任何类型的指针都可以指向空。没用具体的类型,只知道起始位置,所占内存的长度无法得知,因此也无法正确地获取数据。若要获取数据,只好再赋类型给void指针。

Function pointers

• In some programming languages, functions are first classvariables (can be passed to functions, returned from functions etc.).

• In C, function itself is not a variable. But it is possible todeclare pointer to functions.

• Question: What are some scenarios where you want to passpointers to functions?

• Declaration examples:

• int (∗fp )( int ) /∗notice the () ∗/

• int (∗fp )( void∗,void∗)

• Function pointers can be assigned, pass to and from functions,placed in arrays etc.

• Can assign to a function pointer:

int (∗fp )( void ∗, void ∗) = strcmp_wrapper; or

int (∗fp )( void ∗, void ∗) = &strcmp_wrapper;

• Can call from function pointer: (str1 and str2 arestrings)

int ret = fp( str1 , str2 ); or

int ret = (∗fp )( str1 , str2 );

函数不能“变化”,即不能像变量一样被赋值。这也挺好理解,函数的作用就是复用一组操作,从而减轻程序员的工作量和减小程序体积,防止“重复发明轮子”。但这样就不能像变量一样“变化”,即程序里使用某个方法后,不能动态地用赋值方式改变。虽然不能将方法代码直接赋值,但我们可以获取它的指针,通过指针来调用方法。无论什么样的指针,其总是一个“指针”,即可以在内存中随意保存指针,并且不会对程序体积有较大影响且不影响方法的复用。

从方法指针也可以看出,方法这种“类型”由哪些要素决定。至少包括返回类型和参数类型。

使用函数指针是因为要灵活性,即不确定用哪个方法,因此在框架中和类库中应该有大量的应用。

Callbacks

Definition: Callback is a piece of executable code passed tofunctions. In C, callbacks are implemented by passing functionpointers.

Example:

void qsort(void∗ arr , int num,int size ,int (∗fp )( void∗pa,void∗pb))

• qsort() function from the standard library can be sort anarray of any datatype.

• Question: How does it do that? callbacks.

• qsort() calls a function whenever a comparison needs to bedone.

• The function takes two arguments and returns (<0,0,>0)depending on the relative order of the two items.

回调函数是传给方法的一组可执行代码,即是一个指令流和数据流而不仅仅是数据。在函数式编程语言中,遍布回调函数,因为其运行机制就是不断地解函数,并且里面的函数是可以像变量一样赋值。

Hash table

Hash tables (hashmaps) combine linked list and arrays to providean efficient data structure for storing dynamic data. Hash tablesare commonly implemented as an array of linked lists (hash tableswith chaining).


哈希表将一个稀疏的数据分布映射到一个较为密集的分布,并且通过映射函数达到或接近随机存储的速度。hash的效率取决于映射函数。

内容概要:本文详细介绍了基于FPGA的144输出通道可切换电压源系统的设计与实现,涵盖系统总体架构、FPGA硬件设计、上位机软件设计以及系统集成方案。系统由上位机控制软件(PC端)、FPGA控制核心和高压输出模块(144通道)三部分组成。FPGA硬件设计部分详细描述了Verilog代码实现,包括PWM生成模块、UART通信模块和温度监控模块。硬件设计说明中提及了FPGA选型、PWM生成方式、通信接口、高压输出模块和保护电路的设计要点。上位机软件采用Python编写,实现了设备连接、命令发送、序列控制等功能,并提供了一个图形用户界面(GUI)用于方便的操作和配置。 适合人群:具备一定硬件设计和编程基础的电子工程师、FPGA开发者及科研人员。 使用场景及目标:①适用于需要精确控制多通道电压输出的实验环境或工业应用场景;②帮助用户理解和掌握FPGA在复杂控制系统中的应用,包括PWM控制、UART通信及多通道信号处理;③为研究人员提供一个可扩展的平台,用于测试和验证不同的电压源控制算法和策略。 阅读建议:由于涉及硬件和软件两方面的内容,建议读者先熟悉FPGA基础知识和Verilog语言,同时具备一定的Python编程经验。在阅读过程中,应结合硬件电路图和代码注释,逐步理解系统的各个组成部分及其相互关系。此外,实际动手搭建和调试该系统将有助于加深对整个设计的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值