了解如何将main(){}中出现的一大块代码转换为C中的一个或多个函数(Understanding how to turn a chunk of code that appears in main()

编程入门 行业动态 更新时间:2024-10-11 21:24:56
了解如何将main(){}中出现的一大块代码转换为C中的一个或多个函数(Understanding how to turn a chunk of code that appears in main(){} into a function or functions in C)

我正在研究用户空间驱动程序,它通过发送和接收报告(它是一个隐藏设备)从设备读取数据。 我对C很新,所以如果你看到任何愚蠢的话,请跟我一起玩:)

这是我的初始代码http://pastebin.com/ufbvziUR

编辑注意:从答案和评论的声音看起来我将需要将我的C代码包装在一些Objective-C中,因为将使用该驱动程序的应用程序是用Objective-C编写的。

到目前为止,我刚刚将所有代码添加到main()函数中,并且我能够获取数据并将其打印到日志窗口,其主要目的是注销缓冲区数组。 这段代码虽然是一个更大的应用程序的一部分,但需要通过调用而不是仅仅自动运行来运行。

所以我想我会把所有出现在main()的代码包装在一个名为getData()的大函数中。 我希望在运行此函数时返回一个名为char buffer[]的数组(一个充满字节的char数组)。 我最初的想法是声明它会是这样的,因为我知道你不能真正从一个函数返回一个数组只有一个指针所以我只是让调用者分配缓冲区并传递它的大小。

char *getData(int user){ unsigned char *buffer = malloc(2800); // ... do all the stuff return buffer; }

然后在main()

int main(){ int user = 1; unsigned char *buffer = getData(user); }

这是一种正确的方法吗?

这里有几件事我觉得不对劲。 首先是将那么多代码包装到一个函数中,当我的一个错误检查returns 1;时,我不太确定如何突破该函数returns 1; 因为这个函数需要返回一个数组。 我还是C的新手,当我没有对象或类可以使用时,我对如何处理这个问题感到困惑。 非常感谢所有的指导和建议!

I'm working on a user-space driver which reads data off of a device by sending and receiving reports (it's a hid device).

Here is my initial code http://pastebin.com/ufbvziUR

EDIT NOTE: From the sound of the answers and comments it would seem I will need to wrap my C code inside of some Objective-C since the app that will be consuming this driver is written in Objective-C.

As of now I have just added all the code into the main() function and I'm able to grab data and print it out to the log window with the main purpose of logging out the buffer array. This code will be apart of a much larger app though and will need to be ran by getting called rather than just automatically running.

So I figured I would wrap all the code that appears in main() inside of a large function called getData(). I want to return an array called char buffer[] (a char array full of bytes) when this function get run. My initial thought to declare it would be like so since I know you cannot really return an array from a function only a pointer so I just let the caller allocate the buffer and pass it the size.

char *getData(int user){ unsigned char *buffer = malloc(2800); // ... do all the stuff return buffer; }

Then in main()

int main(){ int user = 1; unsigned char *buffer = getData(user); }

Is this a proper approach?

A couple of things feel wrong to me here. First is wrapping that much code into a single function and two I'm not quite sure how to break out of the function when one of my error checks returns 1; since this function will need to return an array. I'm still really new to C and am getting confused on how to approach this when I don't have objects or classes to work with.

最满意答案

void getData(unsigned char *buffer, int user){...}

定义一个不返回任何内容的函数。

如果你想返回一些值 - 比如错误代码,你需要一个返回int的函数。

int getData(unsigned char *buffer, int user){...}

要使函数以不同的方式返回void然后到达函数的末尾,您也可以使用return但不使用任何参数。

正如您已经注意到的那样,您没有将数组传递给函数,因此根本没有理由返回它。 如果需要,可以返回指针,但不需要这样做。 主函数无论如何都知道数组的位置。

一般认为保持主要尽可能短的好习惯。 您还可以将getData划分为更小的函数,这将使代码更具可读性。 例如,您可以使用# pragma mark - ...每个块# pragma mark - ...一个单独的函数。 或者甚至更好,看看程序的任何部分是否做了相同或类似的事情。 然后,您可以将此功能概括为一个功能并多次使用它。

void getData(unsigned char *buffer, int user){...}

Defines a function that is not returning anything.

If you want to return some value - like for instance error code, you need a function returning an int.

int getData(unsigned char *buffer, int user){...}

To get out of function returning void differently then reaching the end of the function, you can also use return but then without any arguments.

As you've already noticed you aren't passing array to the function so there is no reason to return it at all. You could return the pointer if you wanted but the is no need to do so. The main function knows where the array is anyway.

It's generally considered a good habit to keep main as short as possible. You can also divide getData into smaller functions which would make the code more readable. You could for instance make every chunk marked by # pragma mark - ... a separate function. Or even better, see whether there are any parts of the program that are doing the same or similar thing. Then you can generalize this functionality into one function and use it multiple times.

更多推荐

本文发布于:2023-08-07 02:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459089.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   转换为   如何将   函数   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!