我试图理解这个来自Tcl文档的代码
typedef int Tcl_DriverOutputProc(
ClientData instanceData,
const char *buf,
int toWrite,
int *errorCodePtr);
Run Code Online (Sandbox Code Playgroud)
我知道typedef的目的是为现有类型分配替代名称,那么为什么需要将typedef int赋予函数?这怎么用?
是否可以创建一个函数而不是两个具有相同目的但不同参数类型的函数?
我有两个用C编写的函数,它们将图像从RGB转换为HSV:
void png_rgb2hsv(pPNG_DATA data);
void jpg_rgb2hsv(pJPEG_DATA data);
Run Code Online (Sandbox Code Playgroud)
它们完全相同:它们采用数据 - > row_pointers并将其循环到循环数据 - >高度时间.然后它转换data-> row_pointers引用的值.这是它的工作原理.但唯一的区别是数据结构使用不同的类型.对于同样的事情,使用两个函数似乎毫无意义.特别是当我为更多色彩空间添加更多功能时.
如何在C的实践中解决这个程序设计问题?
更新: 大多数读者都不理解这个问题.我没有问过重载.这是关于设计的问题.我问是否可以删除冗余功能,"冗余"代码.因为两个函数都使用相同的代码,所以做同样的事情,但函数参数中的类型是不同的,因为一个类型来自libjpeg而第二个类型来自libpng.我发现它是不可能的,因为它意味着将一个变量用于两种不同的类型.
我对将函数指针赋给变量的正确语法有点困惑.如果我有一个函数foo
int foo();
Run Code Online (Sandbox Code Playgroud)
我正在将指向foo的指针分配给变量栏
void * bar;
Run Code Online (Sandbox Code Playgroud)
如果我使用它似乎并不重要
bar = foo;
// or
bar = &foo;
Run Code Online (Sandbox Code Playgroud)
在我看来,其中只有一个应该是正确的还是我错过了什么?
目标是更改事件循环中的行为,具体取决于是否打开或关闭复选框.我能想到的最简单的方法就是每次运行循环时测试复选框状态.
// if-statement
void action() { /* ... */ }
void someLoop() {
if (checkboxTrue) {
action();
}
// ... other stuff
}
Run Code Online (Sandbox Code Playgroud)
如果使用函数指针,代码是否会更高效,更清晰或更好?像这样:
// function pointer
void action() { /* ... */ }
void empty() {}
void (*actionPtr)();
void checkboxChanged(int val) {
if (val == 1)
actionPtr = &realAction;
else
actionPtr = ∅
}
void someLoop() {
(*actionPtr)();
// ... other stuff
}
Run Code Online (Sandbox Code Playgroud) c performance if-statement function-pointers data-oriented-design
int eax = ((int(*)())("\xc3 <- This returns the value of the EAX register"))();
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?字符串被转换为函数指针
你能根据整数是一个函数来调用函数吗?
这就是我的意思:
#include <iostream>
using namespace std;
int whichFunction;
int main()
{
cout << "Which function do you want to call?";
cin >> whichFunction;
function[whichFunction]();
//If you entered 1, it would call function1 - same with 2, 3
//or Hihihi (of course only when whichFunction would be a string)
}
void function1()
{
cout << "Function No. 1 was called!";
}
void function2()
{
cout << "Function No. 2 was called!";
}
void functionHihihi()
{
cout << "Function Hihihi was …Run Code Online (Sandbox Code Playgroud) (我知道C不是以功能性的方式使用.但是,在学习了函数式编程之后,我的思维方式也有所不同.)
鉴于这些限制:
我们可以想到一种提升函数f以获取额外参数以适合原型的方法,例如当f执行时,这个新参数会被忽略吗?
这里详细介绍了我想要做的事情:
我想要拟合一个函数f,其类型是:
f :: void f(char *s)
Run Code Online (Sandbox Code Playgroud)
在一个函数g中,它接受一个函数作为参数(称之为arg),其类型为:
arg :: void f(unsigned int i, char *s)
Run Code Online (Sandbox Code Playgroud)
因此,g的类型是:
g :: void g(void (*f) (unsigned int, char))
Run Code Online (Sandbox Code Playgroud)
haskell中的解决方案如下:
g (const f)
Run Code Online (Sandbox Code Playgroud)
这是否可能,也许是某种宏观魔法?
编辑:为了更好地理解,这里是实际的代码.需要完成ft_striter的主体.目的是:将函数f应用于字符串的每个字符,使用或不使用其索引i.
void ft_striter(char *s, void (*f) (char *s))
{
ft_striteri(?);
}
static void ft_striteri_helper(char *s, unsigned int i, void (*f)(unsigned int, char*))
{
if (*s)
{
f(i, s);
ft_striteri_helper(s + 1, i + 1, f);
}
}
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{ …Run Code Online (Sandbox Code Playgroud) 我想getname在我的内核模块中使用该函数。它没有导出。由于我现在遇到了这个问题,我想知道如何访问和使用任何未导出的内核符号。我认为使用一个符号所需的步骤会有所不同,因此我想看看如何为类型(例如,结构)、变量、指针表(如系统调用表)和一个函数。如何在以下任一情况下完成这些操作:
System.map或 中知道符号的地址时/proc/kallsyms。kallsyms_lookup_name检索它时。我目前知道如何劫持系统调用,这需要声明类似
asmlinkage <return_type> (*<name_for_system_call>)(<the types of the its arguments separated by commas>);
Run Code Online (Sandbox Code Playgroud)
会使用这样的东西吗?在这个对另一个问题的回答中,海报展示的例子是
#include <linux/kallsyms.h>
static void (*machine_power_off_p)(void);
machine_power_off = (void*) kallsyms_lookup_name("machine_power_off");
Run Code Online (Sandbox Code Playgroud)
但是如果符号返回一个指针呢?我会在 的左边放一个星号(*machine_power_off_p)吗?
我有一个功能,我想通过各种功能
int func1(char *ptr)
{
printf("%s",ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和其他我想调用func1的函数
int func2(void *i)
{
//call i
//here i point to function func1
//and do something with return value of i
}
Run Code Online (Sandbox Code Playgroud)
那么,我应该如何在main()中调用它?
int main()
{
void *d;
//SOMETHING wrong in next four line
d=&func1("abcd");
func2(d);
d=&func1("xyz");
func2(d);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我最近在读代码,发现函数指针写成:
int (*fn_pointer ( this_args ))( this_args )
Run Code Online (Sandbox Code Playgroud)
我经常会遇到这样的函数指针:
return_type (*fn_pointer ) (arguments);
Run Code Online (Sandbox Code Playgroud)
这里讨论类似的事情:
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我有什么区别,这是如何工作的?
c ×9
c++ ×2
function ×2
pointers ×2
casting ×1
clang ×1
haskell ×1
if-statement ×1
linux ×1
linux-kernel ×1
performance ×1
tcl ×1
types ×1