I have a problem in C while parsing the argv Variable. This is the task:
I got some Parameters form the Command line into my C programm. One of them looks like "-r=20-5". There I have to find the "r". this works with:
if (argv[i][0] != 0 && argv[i][1] != 0 && argv[i][2] != 0 &&
argv[i][0] == '-' && argv[i][2] == '=') { /* Check the Variables */
switch (argv[i][1]) {
case 'r':
/* what have to be here? */
break;
}
Now I have the "r". But I need the 20 and the 5 in two different variables too. Here is what i thougt about, but it did't work. I tried something with
strsep(argv[i]+3, "-");
But my Compiler (Xcode) throws a warning (warning: passing argument 1 of 'strsep' from incompatible pointer type)
Does anyone has any idea (or link) how I can solve the problem?
After it's solved there was still a warning, so I was answered to post my entire new code:
int parse(int argc, const char *argv[]) {
int i, x, y;
int intTmp;
long longTmp;
char *strTmp;
char *end;
for (i = 1; i < argc; i++) {
/* check for (-?=) */
if (argv[i][0] != 0 && argv[i][1] != 0 && argv[i][2] != 0 &&
argv[i][0] == '-' && argv[i][2] == '=') {
/* IGNORE THIS
* longTmp = strtol(argv[i]+3, &end, 10);
* intTmp = analyseEndptr(longTmp, argv[i]+3, end);
*/
switch (argv[i][1]) {
case 'r':
strTmp = argv[i]+3; /* <= Here I got the warning */
x = atoi(strsep(&strTmp, "-"));
y = atoi(strsep(&strTmp, "-"));
printf("x=%d, y=%d\n", x, y);
break;
}
}
}
}
The warning is: warning: passing argument 1 of 'strsep' from incompatible pointer type
My compiler is: gcc on MacOS using XCode as IDE