I am currently studying how to work with pointers in C, and I have several questions if you dont mind.
I am trying to process information from files, therefore - to maintain modularity - I want to "send my files" into several helper methods.
My questions are:
I know that if I create these file pointers inside a method - they are kept inside the stack. Is it problematic (In a sense that it may end in unwanted behaviours) to send pointers from the stack memory to other methods?
I know that if i'll send the pointers of the files to other methods, it will actually create a copy of them, which means that changing them inside the method would not do anything at all.
Thats why im asking about "pointers to pointers", Ive read (but didn't quite get it) that I could send the helper methods pointers to the file pointers - and in that way, I would work on my actual files, rather than some local copy.
Lets say that mainFunction is my main method (it is not in the "real" main method) which I use for processing files
void helperFunction1(FILE* file1, FILE* file2)
{
fclose(outputFile);
fclose(inputFile);
}
void helperFunction2(FILE* file1, FILE* file2)
{
**write some stuff into both files**
}
void mainFunction()
{
FILE* inputFile = fopen(filePath, "r");
FILE* outputFile = fopen(OUTPUT_FILE_NAME, "w");
helperFunction2(inputFile, outputFile);
helperFunction1(inputFile, outputFile);
}
Would the "real" inputFile and outputFile get closed from calling to helpFunction1?
Would the "real" inputFile and outputFile get modified (get some stuff written into them) from calling to helpFunction2?
I would love to get some insight / answers for my questions (I hope they are valid and not too pushy) and also I would love to get a brief explanation on how to work with "pointers to pointers" in order to modify data, rather than modifying some copies of it.
helperFunction1you need to use the names of the arguments (file1andfile2) and not their names in the calling function.if i'll send the pointers of the files to other methods, it will actually create a copy of them, which means that changing them inside the method would not do anything at all.-- Your goal isn't to change the pointers; it is to change the data that the pointers point to.