简体   繁体   中英

Understanding how to turn a chunk of code that appears in main(){} into a function or functions in 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){...}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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