+ 1
[C/C++] Argc and argv in struct = bad idea?
Is it a bad habit if I create a structure which contains argc, argv and other related variables (like custom bool arg_error)? Because in my project I find myself passing too many arguments to functions (and frequenly argv, argc), and creating one structure of mentioned variables would make the code easuer to read.
9 Respuestas
+ 2
I think the internal representation of these values depends on your project requirements; I mean, you can structure the data of your code into more abstract data types for you to develop better and reliably your code (that's always a very good idea), but argc and argv (and a third parameter named envp) are the standard arguments that any new process in the operating system will wait for, so if you ensure the execution satisfy the correct way of process invocation, everything will work properly and your code will be more understandable and maintainful
+ 2
I suggest you encapsulating them into a data structure, but I warned you about you can't use that data structure directly for process invocation (as the invocation argument) , because the C syntax for that requires argc and argv arguments explicitly, and that's not a customizable design decision, so you will access that attributes of your data structure (the fields which store the argc and the argv values) and use them as the arguments of the new process' invocation
+ 2
Maybe I'm explaining badly, sorry. I mean, when you invoke a process, you invoke its main, which is a function defined as "int main(int argc, char* argv[])" , and although you don't call it directly, the operating system's interface for process invocation will expect that argc and argv parameters, so if you define a data structure like "struct ProccessData{... int argc; char* argv[];...};", you can't invoke that main function as "main(struct ProccessData pd)" through the operating system dedicated function, because it expects other types as arguments, so you must use "main(pd.argc, pd.argv);", being pd an instance of your data structure, for the process invocation
+ 2
Yes, I think it's a very good idea, specially with a language like C (it's a high level language, but often deals with low level elements which difficult the code understandability). What you're saying will improve your code readability and maintainability
+ 1
So you suggest that because these parameters are “standard” I should not create a structure for them? even despite that my function definition will include more parameters? I thought that if I “encapsulated” them with some other related variables, it would make the code more readable... Or am I misunderstanding your claim?
+ 1
I hope this last message to be helpful
+ 1
Yes, thank you, I think I know what you mean... Well what you described is not something I would do, I would just use that structure to “wrap” them together, maybe with other related variables as I mentioned... And then just access them in utility functions as elements of a structure, nothing more... Meaning that instead of sending 2/3 parameters, I would send just the structure..Is this something I could do? or it is not that useful
+ 1
Thank you again very much Martin you helped me a lot, I really appreciate it :)
+ 1
You're welcome, I hope everything goes well in your project :)