| ▲ | Progge 11 hours ago | |||||||||||||||||||
Long time ago I wrote C. Could anyone fill me in why the first code snippet is arg parsing the way it is? int main(int argc, char* argv[]) {
}Why not if (argc > 1 && strcmp(argv[1], "-r") == 0) {
}for example? | ||||||||||||||||||||
| ▲ | tapete2 11 hours ago | parent | next [-] | |||||||||||||||||||
It doesn't even make sense to use strchr for determining the position of 'r', when the code checks that the position of '-' is at index 0. Your solution is perfectly fine. Even if you don't have access to strchr for some reason, the original snippet is really convoluted. You could just write (strlen(argv[1]) > 1 && argv[1][0] == '-' && argv[1][0] == 'r') if you really want to. | ||||||||||||||||||||
| ||||||||||||||||||||
| ▲ | CerryuDu 2 hours ago | parent | prev | next [-] | |||||||||||||||||||
Not to mention the potential signed integer overflow in (*right - *left) and (*left - *right), which is undefined behavior. And even if you rely on common two's complement wraparound, the result may be wrong; for example, (INT_MAX-(-1)) should mathematically yield a positive value, but the function will produce INT_MIN, which is negative. And then we have this "modern" way of spelling pointers, "const int* right" (note the space). In C, declaration syntax mirrors use, so it should be "const int *right", because "*right" is a "const int". I feel too old for this shit. :( | ||||||||||||||||||||
| ▲ | Joker_vD 8 hours ago | parent | prev | next [-] | |||||||||||||||||||
I suspect it was adopted from a bigger snippet that had support for parsing things like "-abc" as "-a -b -c", etc. | ||||||||||||||||||||
| ▲ | 4 hours ago | parent | prev [-] | |||||||||||||||||||
| [deleted] | ||||||||||||||||||||