0

Below is a piece of the code from copy.c in pset4. I am interpreting it in the following way:

  1. the name of the input file is being entered as a string to be stored at argv[1].

  2. a pointer to argv[1][0] is declared (char* infile)

  3. the pointer 'infile' is passed to fopen().

Does fopen read through all of argv[1] until it encounters an implicit NULL terminator (\0)? Otherwise how does it know what the entire name of the file is? Is this how functions typically behave when passed an array (do they read through the array until encountering a '/0'?)

    int main(int argc, char* argv[])
    {
        // ensure proper usage
        if (argc != 3)
        {
            printf("Usage: ./copy infile outfile\n");
            return 1;
        }

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

        // open input file 
        FILE* inptr = fopen(infile, "r");

       ...
   }
0

Yes, you are correct. A char* infile will point to the starting index of the array, and then infile will hold all the array elements it encounters before a \0.

http://ideone.com/PKVO5r

Although this has nothing to do with functions. We are passing a variable to a function(i.e. a char array here), and it takes whole array as its parameter. In the function definition, the specified parameter takes the values of the parameters that were passed during the function call, and thats done. However, the parameters can also be passed by reference, instead of being passed by value.

| improve this answer | |

You must log in to answer this question.

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