我有一个功能,我想通过各种功能
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 (*function)(int, float);
Run Code Online (Sandbox Code Playgroud)
我不太了解 它有两个参数,但没有名称。这是如何运作的?我的意思是,在声明这样的函数时:
int f(int x, int y) {
return x+y;
}
Run Code Online (Sandbox Code Playgroud)
没有标识符怎么办?我注意到这行不通,甚至在第一行给出了一个编译器错误,说
int f(int, int) {
return /* What should I even write here? */ ;
}
Run Code Online (Sandbox Code Playgroud)
我得到两个错误:
f.c:1:7: error: parameter name omitted
int f(int, int)
^~~
f.c:1:7: error: parameter name omitted
int f(int, int)
^~~
Run Code Online (Sandbox Code Playgroud) 我已经阅读了有关函数指针语法的内容,但无法在我的代码中工作。
该视频解释了语法是:
type func0(type0 arg0, ..., typen argn){
// do something
return a_thing;
}
Run Code Online (Sandbox Code Playgroud)
然后在另一个函数中传递函数指针是:
type func1((*func0)(type0 arg0, ..., typen argn), another args){
//do something
func0(arg0, ..., argn);
return a_thing;
}
Run Code Online (Sandbox Code Playgroud)
但是后来,我读了第一个关于C 中的函数指针如何工作?,语法为:
type func1((*func0)(type0 arg0, ..., typen argn), another args) {
//do something
(*func0)(arg0, ..., argn);
return a_thing;
}
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为您正在取消引用您正在传递的指针。最后, C 函数指针调用语法中的第一个答案解释了该语法可以是:
&func
func
*func
**func
Run Code Online (Sandbox Code Playgroud)
我不明白哪些信息是正确的。我有代码:
double *func1((*func0)(double), double *var) {
double *temp = malloc(some_size);
for(int i = 0; i < some_cycles; ++i) …Run Code Online (Sandbox Code Playgroud) 我正在使用以下简单代码重新访问C中的函数指针:
unsigned TestFn(unsigned arg)
{
return arg+7;
}
unsigned Caller(unsigned (*FuncPtr)(unsigned), unsigned arg)
{
return (*FuncPtr)(arg);
}
Run Code Online (Sandbox Code Playgroud)
我用它来称呼它
Caller(TestFn, 7) //and
Caller(&TestFn, 7)
Run Code Online (Sandbox Code Playgroud)
两者都给出了相同的输出:14.这是什么解释.我之前一直在使用第二种方式.
在阅读此文章中,我碰到的函数指针的以下声明.我没有使用过这样的声明,但我解释它的方式是:当取消引用时,functionFactory的返回值是一个接受2个int的函数并返回一个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)
我很想知道这样的声明是针对这种情况还是有一种我错过的通用方法.
我的意思是我们通常会看到类似的声明
<returnType> funcName(listOfArgs){}
Run Code Online (Sandbox Code Playgroud)
这个出现在联盟之外.请有人详细说明.
我需要在我的C程序中使用简单的ELF文件.我不能使用任何外部库,但我可以使用elf.h.
让我们把hello.o文件作为源代码:
int Hello() { return 3; }
Run Code Online (Sandbox Code Playgroud)
我怎样才能访问Hello只有hello.o文件的ohter C程序?
我应该把它加载到内存中使用mmap或者像这样.最后我需要使用更复杂的ELF文件,但我现在不知道,如何开始.
更新:我需要按照我描述的方式执行此操作,因为它是出于学习目的.整个问题比我描述的更复杂.
对于这个问题假设我需要编写方法:
int HelloFromElfO(const char* helloFile);
Run Code Online (Sandbox Code Playgroud)
这将执行Hello在中实现的功能helloFile.
我不想要完整的答案.我不需要任何代码.我需要一些东西开始.
我有关于ELF文件结构的基本知识,但我不知道如何在没有任何解析器的情况下使用二进制文件在C中工作或者像这样.
UPDATE2:好的,像readelf这样的应用程序非常复杂.所以也许我尝试这种方式:再说一遍,我已经hello.o映射到内存了ptr.如何获得指向Hello函数的指针?
如何从中获取任何结构化数据hello.o?我的意思是,不是纯字节,而是我可以使用的东西.
我正在查看以下代码:
string toUpper(string s) {
string result;
int (*toupperp)(int) = &toupper; // toupper is overloaded
transform(begin(s), end(s), back_inserter(result), toupperp);
return result;
}
Run Code Online (Sandbox Code Playgroud)
我被这条线弄糊涂了:
int (*toupperp)(int) = &toupper; // toupper is overloaded
Run Code Online (Sandbox Code Playgroud)
为什么这条线路必要?
我相信&从内存中检索指向某事物的指针.但是toupper,函数的名称已经是一个指针,不是吗?为什么我们不能这样做:
int (*toupperp)(int) = toupper;
Run Code Online (Sandbox Code Playgroud)
3. int如果函数被重载,为什么它被重载string?
是否可以为头文件中声明的相同函数提供2个(或更多)不同的实现?我举一个例子-假设我们有一个名为一个头文件common.h和2个名为源文件src1.c和src2.c.
//lots of common function declarations implemented in some file common.c
int func(int a, int b);
Run Code Online (Sandbox Code Playgroud)
#include "common.h"
int func(int a, int b)
{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
#include "common.h"
int func(int a, int b)
{
return a*b;
}
Run Code Online (Sandbox Code Playgroud)
假设我希望每个源文件都使用其本地版本func().有可能这样做吗?
我想创建一个函数,该函数需要一个数组和一个函数,并在数组中的每个元素上调用该函数
我一直在寻找解决方案,但它们似乎都使用宏,而如果可能的话,我希望使用函数。
我正在寻找可以像下面这样工作的东西
void print_string()
{
printf("%d\n", num);
}
int array[] = { 1, 2, 3, 4, NULL }; // So the foreach function knows when it has reached the end
for_each_function(array, print_number);
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
编辑:它需要是通用的,因此可能需要一个宏
我正在尝试数组指针和函数指针的一些随机声明。我被打中了。
#include<bits/stdc++.h>
int add(int a,int b){
return 1729; }
int main(){
int (abc)(int,int);
abc = add;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
发出的错误是:
#include<bits/stdc++.h>
int add(int a,int b){
return 1729; }
int main(){
int (abc)(int,int);
abc = add;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但随着这两个abc和add是同一类型的,它为什么要转变?还是可以将函数分配给新变量?如果是这样,该怎么做?