简体   繁体   English

将函数指针和int引用传递给另一个函数

[英]Passing function pointers & int references to another function

Just started working on ac project. 刚开始从事ac项目。 Need help with passing function pointers/macro functions/etc. 在传递函数指针/宏函数/等方面需要帮助。 I'm a php & python OO guy, but new to c. 我是php&python OO对象,但对c还是陌生的。 I tried to generalize the example for this post. 我试图归纳本文的示例。 I have a main.c with a lib for the Axon microcontroller I'm working with. 我有一个main.c,其中包含我正在使用的Axon微控制器的库。 Works like a charm with everything in main.c. 与main.c中的所有内容一样具有魅力。 I need to move some of the functionality out of main to more organized lib files as my code grows. 随着代码的增长,我需要将一些功能从主要功能移出到更有条理的lib文件中。 The base microcontroller lib creates a macro function that allows me to send data to the microcontroller to make a servo move left or right. 基本的微控制器库创建了一个宏函数,该函数使我可以将数据发送到微控制器以使伺服器向左或向右移动。 I now need to create a servo specific file (HS-422.c) that will will allow me to pass references/pointers(?) to a generic function that will execute for each servo to ease on code duplication. 现在,我需要创建一个伺服专用文件(HS-422.c),该文件将允许我将引用/指针(?)传递给通用函数,该函数将为每个伺服器执行,以简化代码复制。

Keep in mind I'm only focused on passing macros/functions/variable references to other functions and have them called / set. 请记住,我只专注于将宏/函数/变量引用传递给其他函数,并将它们称为/设置。 The other basics of c I understand. 我了解c的其他基础知识。 I must have tried a 100 different ways to make this work today with no luck. 我一定尝试了100种不同的方法来使这项工作今天顺利进行。 So just wrote a simplified version hoping you might get an idea of what I'm attempting. 因此,只写了一个简化版本,希望您对我正在尝试的内容有所了解。

Thank you for your help! 谢谢您的帮助!

/*
 * main.h 
 * I'm trying to make a pointer or reference to the macro.  
 * The original file had:
 * #define servo1(position) servo(PORTE,2,position); 
 */

 // servo is a macro defined in another microcontroller file
 #define (void)(*servo1)(position) servo(PORTE,2,position); 
 #define (void)(*servo2)(position) servo(PORTE,3,position); 


/* main.c */

// init main functions
void servo_scan(void);

// init vars
int servo1_location = 0;
int servo2_location = 0;

int main(void)
{
  for(;;)
  {
     servo_turn();
  }
}

// get the servos to turn
void servo_turn(void)
{
  turn_servo( *servo1, &servo1_location, 200);
  turn_servo( *servo2, &servo2_location, 950);
}


/* HS-422.c */
void turn_servo(void (*servo)(int position), int &currentLocation, int newLocation)
{
  // turning
  for(uint16_t  i=&currentLocation; i<newLocation; i=i+10)
  {
    // turn servo
    // hoping the specifc passed servo# pointer gets called
    *servo(i);     

    // set value by reference to origional servo#_location var. making sure.
    &currentLocation = i; 

    // pause
    delay_ms(20);
  }
}

It's not really clear to me exactly what you're trying to achieve, but what is clear is that you don't really understand the concept of pointers/references in C - so I'll try to clarify, and hopefully that will help you implement what you need. 对我来说,确切的含义不是很清楚,但是很明显,您并不真正了解C中的指针/引用的概念-因此,我将尽力澄清一下,希望对您有所帮助。实现您所需要的。

Firstly, there is no such thing as a "reference" in C. The only alternative to passing by value is to pass a pointer. 首先,在C语言中没有“引用”之类的东西。传递值的唯一替代方法是传递指针。 A pointer is basically just a memory address, and you can get a pointer (memory address) to a variable using the & (address of) operator. 指针基本上只是一个内存地址,您可以使用& (地址)运算符获取指向变量的指针(内存地址)。 When passing a pointer variable to a function, you do something like the following: 将指针变量传递给函数时,您将执行以下操作:

Given a function which takes a pointer: 给定一个带有指针的函数:

int foo(int* pointer);

You would pass the memory address of an int variable to this function like so: 您将像这样将int变量的内存地址传递给此函数:

int x = 10;
foo(&x);

So right off the bat, you can see that your function definition above is wrong: 因此,马上就能发现上面的函数定义是错误的:

void turn_servo(void (*servo)(int position), int &currentLocation, int newLocation);

This is simply a syntax error. 这只是语法错误。 It will not compile because of the int &currentLocation. 由于int &currentLocation. ,它将无法编译int &currentLocation. The & operator is used to take the address of a variable. &运算符用于获取变量的地址。 It can't be used in a function parameter. 不能在功能参数中使用。 If you want a "reference" to currentLocation , you need to pass in a pointer, so your function parameters should be written as: 如果要“引用” currentLocation ,则需要传递一个指针,因此函数参数应写为:

void turn_servo(void (*servo)(int position), int* currentLocation, int newLocation);

Secondly, when you want to modify the value pointed to by the currentLocation pointer, you need to use the * operator to dereference the pointer. 其次,当您要修改currentLocation指针所指向的值时,需要使用*运算符取消对指针的引用 So, the line where you set currentLocation is not correct. 因此,您设置currentLocation的行不正确。 What you want to say is: 您想说的是:

// set value by to origional servo#_location var. making sure.
*currentLocation = i; 

And of course, the line: 当然,这一行:

  for(uint16_t i=&currentLocation; i<newLocation; i=i+10)

should be: 应该:

  for(uint16_t i= *currentLocation; i<newLocation; i=i+10)

Note that in your original code you use the & operator in both cases, which takes the address of a variable. 请注意,在原始代码中,在两种情况下都使用&运算符,后者使用变量的地址。 Since currentLocation is already a memory address, this would result in taking the address of an address , also known as a pointer-to-a-pointer, which is certainly not what you want here. 由于currentLocation已经是一个内存地址,因此这将导致获取地址的地址 ,也称为指针到指针,这当然不是您想要的。

Finally, the phrase "pointer or reference to the macro" is completely nonsensical. 最后,短语“对宏的指针或引用”完全是荒谬的。 A macro is not a function. 不是函数。 It is more like a meta-function: essentially it is a template used by the C preprocessor to generate further source code. 它更像一个元函数:本质上,它是C预处理器用来生成更多源代码的模板。 The C preprocessor is invoked before the compilation phase, and basically acts as a find/replace mechanism in the source code. C预处理程序在编译阶段之前被调用,并且基本上充当源代码中的查找/替换机制。 You can't have a pointer to a macro, because for all intents and purposes macros don't even exist in the compilation phase. 您不能拥有指向宏的指针 ,因为对于所有意图和目的,宏甚至都没有存在于编译阶段。 They are only meaningful to the preprocessor. 它们仅对预处理器有意义。

There may be more here, but ultimately you seem to have a fundamental misunderstanding of pointers (as well as macros) in C, and short of providing a complete tutorial, the best I can do is point out the syntax problems. 这里可能还有更多内容,但是最终您似乎对C中的指针(以及宏)有基本的误解,并且由于没有提供完整的教程,我能做的最好的就是指出语法问题。 I highly recommend you read a good introductory book to C , which will certainly go over pointers, macros, and functions. 我强烈建议您阅读C的入门书籍 ,这当然会涉及指针,宏和函数。

I have picked the main point of your code and have this code below. 我选择了您代码的要点,并在下面提供了此代码。
You may want to modify your #define in your original code. 您可能需要在原始代码中修改#define
Please see the code below: (you can also run this) 请参见下面的代码:(您也可以运行此代码)

void myFunc(int pos);
void myFunc2(int pos);

int main (int argc, const char * argv[]) {


    typedef void (*pFunc)(int);
    pFunc pfArr[2];
    pfArr[0] = &myFunc;
    pfArr[1] = &myFunc2;


    int x = 3;
    int newLoc = 4;
    turn_servo(pfArr[1], x, newLoc);
    turn_servo(pfArr[0], x, newLoc);


    return 0;
}



void turn_servo(void (*servo)(int position), int currentLocation, int newLocation)
{
    printf("\nturn_servo starts");
    printf("\nturn_servo currentLocation: %d", currentLocation);
    printf("\nturn_servo newLocation: %d", newLocation);
    servo(1);

}

void myFunc(int pos)
{
    printf("\nmyFunc starts");
    printf("\nmyFunc pos: %d", pos);
}

void myFunc2(int pos)
{
    printf("\nmyFunc2 starts");
    printf("\nmyFunc2 pos: %d", pos);
}

Your turn_servo() function will now accept two functions as parameter (either myFunc() or myFunc2()). 您的turn_servo()函数现在将接受两个函数作为参数(myFunc()或myFunc2())。
Just get the main point of this code and apply it. 只需获得此代码的要点并应用它即可。 Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM