Hey everyone, I am new to C and I have a simple question regarding a homework assignment I received.
In class, we have learned how to set up the main method so we can convert command line arguments to integers and utilize them. Here is an example of a method we learned to store command line arguments as integers in an array:
Code:
int main (int argc, constchar* argv[]) {int i;
int a[argc - 1];
// Fill the array with the arguments on the command line
for (i = 0; i < argc - 1; i++){
a[i] = atoi(argv[i + 1]);
}
// Now print it out
print_int_array(a, argc - 1);
return0; // Success
}
I understand that there are probably better ways to do it than using the atoi function, but for our purposes, the professor said this was okay.
Now, I am being asked to create a separate function to perform the same task (store integers in an array). I was told I would need to declare the array "out in the main program", and that the function would need to take at least 2 arguments: an array, and an array size.
I am slightly confused as to go about this. Firstly, what should my return value be? My professor gave a hint that it would be important for me to figure out what the return value is, but I don't see why it would be anything else than void.
Also, wouldn't I need a third parameter? Perhaps the integers that are being stored in the array? (But how would I represent this as a parameter)?
Any help/advice would be greatly appreciated! Thanks!