I found a problem. It seems like I always did it in this way, but I know something is wrong and I can't find what. In main I initialize 3 char arrays (array of destination to file to read data, an array of destination to file to write data, and modifier); params is a 2D array (getting list of command-line arguments) and initialize it
int main(int argc,char**argv) {
char** params;
params = (int**)malloc(argc - 1 * sizeof(int*));
for (int i = 1; i < argc; i++) {
params[i - 1] = (int*)malloc(strlen(argv[i]) * sizeof(int));
}
char* inputFile = malloc(sizeof(char));
char* outputFile = malloc(sizeof(char));
char* key = malloc(sizeof(char));
CheckParams(params,argc,argv);
if (!ValidateInput(params,inputFile,outputFile,key,argc)) {
return -1;
}
СheckParams is a function that modifies params array to command-line arguments(this one works fine, I modify my array in a function, and it keep modified in main)
void CheckParams(char** params, int argc, char** argv) {
for (int i = 1; i < argc; i++) {
for (int j = 0; j < strlen(argv[i]); j++) {
params[i - 1][j] = argv[i][j];
}
params[i - 1][strlen(argv[i])] = '\0';
}
}
Then I wrote a function ValidateInput, that have to modify those 3 arrays(inputFile, outputFile, and Key). This function those contains a bit of validation
int ValidateInput(char** params,char* inputFile, char* outputFile, char* key, int argc) {
if (argc >= 4) {
if (strcmp(params[0],"-i")==0 && strcmp(params[2],"-o")==0) {
inputFile = realloc(inputFile, sizeof(char) * strlen(params[1]) + 1 );
for (int i = 0; i < strlen(params[1]); i++) {
inputFile[i] = params[1][i];
}
inputFile[strlen(params[1])] = '\0';
outputFile = realloc(outputFile, sizeof(char) * strlen(params[3]) + 1 );
for (int i = 0; i < strlen(params[3]); i++) {
outputFile[i] = params[3][i];
}
outputFile[strlen(params[3])] = '\0';
if (argc >= 5) {
key = realloc(key, sizeof(char) * strlen(params[4]) + 1 );
for (int i = 0; i < strlen(params[4]); i++) {
key[i] = params[4][i];
}
key[strlen(params[4])] = '\0';
}
else {
key = realloc(key, sizeof(char) * 8);
key = "default";
}
return 1;
}
else {
return 0;
}
}
else {
return 0;
}
}
Maybe I'm so dumb and tired and i cant find so obvious error, but the most interesting thing, that inside ValidateInput function everything works correct, but after returning to main , inputFile and outputFile are garbage, BUT key feels good, just like params. So where is a problem. Ty for help