You are watching: What do you use to separate arguments in a function?
This is the "worker" role header (with separate arguments):
void worker(queue in, queue out, chunk (*process)(chunk));
And this would certainly be the "worker" duty if I had the disagreements in a struct:
void worker(void *ptr) chunk ch, res; struct args *args = ptr; while(q_elements(args->in) > 0) ch = q_remove(args->in); res = args->process(ch); free_chunk(ch); q_insert(args->out, res); And the struct:
struct args int thread_num; queue in; queue out; chunk (*process) (chunk);;Now, my question is: What is the difference in between passing separate disagreements or a reminder to a struct of kind args?
Should ns expect any type of difference? When must I take it one alternative or another?
improve this question
edited may 19 "19 at 15:12
26.8k1414 gold badges4444 silver- badges7777 bronze title
asked might 19 "19 at 12:52
Israel FigueiridoIsrael Figueirido
33622 silver badges1111 bronze badges
include a comment |
1 price 1
energetic oldest Votes
In this case, the advantage of having actually a reminder to a struct, is that once you are developing worker features you commonly use role pointers. Creating role pointers to features with with debate of unspecified argument number and types is possible, but I would not recommend it.
For instance, if we have actually a look at pthread_create, it"s prototype is:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);Here, start_routine is the worker role and arg is the arguments passed to it. It would certainly be virtually impossible to develop a structure that is straightforward to use without this method.
What is the difference in between passing separate debates or a guideline to a struct of kind args? should I expect any kind of difference? When need to I take one choice or another?
As you saw in the over example, if you desire to usage pthread girlfriend simply have to do usage structs.
Another usage is if you want to hide implementation details because that the programmer. That sorts the mimics OOP. Consider having this header:
// foobar.hstruct data;int getId(struct data *data);void setId(struct data *data, int id);Now, for some reason, you don"t want the programmer to access the areas in data directly. So we hide it"s implementation in the source file:
// foobar.cstruct data int id; // more fieldsint getId(struct data *data) return data->id;void setID(struct data *data, int id) data->id = id;Similar reasoning deserve to be offered for a worker function.
See more: Find The Possible Value Or Values Of N In The Quadratic Equation 2N2 – 7N + 6 = 0.
A third reason deserve to be the you have actually LOTS or disagreements to pass. Climate it can improve readability to usage a struct instead.