Here's yet another noob question because... well.. I'm a noob at C programming.
I have the following code:
Code:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#define MAX_PATH_LEN 128
void readFile(char *name);
char filename[MAX_PATH_LEN];
int main(int argc, const char *argv[])
{
int argcount;
for (argcount = 1; argcount < argc; argcount++) {
if (strcmp(argv[argcount], "-f") == 0) {
argcount++;
if (argcount >= argc) {
printf("ERROR! No filename specified.\n");
exit(EXIT_FAILURE);
}
else {
strcpy(filename, argv[argcount]);
}
}
}
printf("My filename: %s\n", filename);
readFile(&filename);
exit(EXIT_SUCCESS);
}
void readFile(char *name)
{
printf("Filename: %s\n", name);
}
If I compile this code it works but I get the following warning:
mytest.c: In function ‘main’:
mytest.c:28: warning: passing argument 1 of ‘readFile’ from incompatible pointer type
If I removed the for loop and hard coded the argv[value] like this:
Code:
int main(int argc, const char *argv[])
{
strcpy(filename, argv[2]); // Assume it's called with -f and a filename following
printf("My filename: %s\n", filename);
readFile(&filename);
exit(EXIT_SUCCESS);
}
I do not get a compilation warning.
I assume the warning is generated because gcc doesn't see a value for argv[#] at compile time, but am I not coding this right? Is there a way I should be coding it to parse argv that wouldn't generate a warning at compile time? Or is my assumption of what the warning means completely wrong?
Thanks in advance for any help!