I strongly recommend not to reinvent the wooden bicycle with triangular wheels, but to use the getopt_long function used in all industrial developments. Something like:
#include <getopt.h> extern char *optarg; extern int optind, opterr, optopt; struct param pr; // Π Π°Π·Π±ΠΎΡ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ² ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ // argc ΠΈ argv - ΠΈΠ· ΡΡΠ½ΠΊΡΠΈΠΈ main. int ParseParam(int argc, char **argv) { int c; int option_index = -1; static struct option long_options[] = { {"duration", required_argument, 0, 't' }, . . . {0, 0, 0, 0 } }; // ΠΠ°Π΄Π°ΡΠΌ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ: memset(&pr, sizeof(struct param), 0); pr.t = 0; . . . // Π Π°Π·Π±ΠΎΡ ΠΎΠΏΡΠΈΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ while (1) { c = getopt_long(argc, argv, "t: . . .",long_options, &option_index); if (c == -1) break; switch (c) { case 't': pr.d = atoi(optarg); break; . . . default: printf("ΠΡΠΈΠ±ΠΎΡΠ½Π°Ρ ΠΎΠΏΡΠΈΡ ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ\n"); return -1; } option_index = -1; } return 0; };
Dots must be replaced with other parameters, and the structure pr is your structure for the entered parameters.
-tand15you can check it with the help of the valueargc- Grundyargcnot 3 show? zero - doesnβt the name of the program count in Si - terangetopt()in the comments to the answer below. If you can pass several parameters, write to the question. If you want to write the parameters yourself, it is obviously necessary toargvover theargvarray in search of the-tvalue and take the next element in the list as a value, checking it for validity / - teran