I am trying to open file in function that loads arguments and then save it into struct containing args for later use in other functions. However I cant seem to be able to save it properly (FILE *A,*B equals 0x0). Here’s whan I’ve got so far:
struct Args
{
int action; /* holds action token (see teacts)*/
int error;
FILE *A,*B; /* files containing matrixes*/
int r,s; /* start coordinates*/
int power,dir; /* used in some other function */
} Args;
void getArgs(int argc, char *argv[],struct Args * args)
{
....
testopen(argv[4],args->A);
....
}
int openFile (const char *arg, FILE *input)
{
input = fopen(arg,"r");
if (input == NULL)
return (1);
else
return (0);
}
int main (int argc, char **argv)
{
struct Args args = {.action = A_ERROR};
getArgs(argc,argv,&args);
....
}
Could anyone tell me what am I doing wrong?
Pointers are passed by value in
C, like everything else. The line:in
openFileonly changes the value of the local variable (argument)inputin that function, it doesn’t changeargs->A.To fix that, you could:
pass a pointer to
argsintoopenFile.pass a pointer to
args->A(making the argument aFILE**):not pass a
FILE*at all, but return one and do:Make sure you return
NULLon error and you can do error checking in the caller.