In this whodunit copy.c the input filenames and output filenames are entered as command line arguments but I couldn't understand that How does inputting the file name will point to the file.

// remember filenames
char *infile = argv[1];
char *outfile = argv[2];

According to this the argv[1] and argv[2] which will be the filenames , will be saved as a character array but they are just the filenames not the pathnames so the char * pointers will point to the memory address where the filenames are stored in the memory.

Please enlighten it's kind of very confusing for me to understand where the pointers will point to ?

1 Answer 1


The notation of the code makes it clear, argv [1] and argv [2] are variables of the pointer type, as the char * infile statement suggests, I do not remember if the theme of the pointers has already been touched on this pset, just say that the variables of type string that are used with the header library of cs50.h, are only a typedef of a variable char * that is to say a pointer to a string, this type of variables do not exist in C, but are handled with pointers. Infile and otfile are therefore two pointers that point to the first element of argv [1] and argv [2] respectively, once we have a pointer to these strings we can use the proper notation of the arrays, or we can write things like fopen (infile) or fclose (infile) etc, I hope this clarifies your doubts


the names of the files are not routes or real files, they are just file names that are going to be created, the actual creation of the file is done with the function fopen () of c, fopen not only opens the files that already exist in the hard drive, also creates them without exist, in this case we will create a file called infile in the folder where we are working, and for that reason we can use only the name of the file as an absolute path.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .