B
Bill Cunningham
I am creating a dice game that returns ints up to the number specified
by argv[1]. Simple enough. But I am also wanting to add a switch, "-a" to be
able to run a routine that adds two numbers and returns the result. So I
want the program to accept "-a" or "a" or "a-" as the switch. Which would be
*(argv[1]) *(argv[1]+1) or *(argv[1]+2) that I would need C to examine. Or
argv[1][0] argv[1][1] argv[1][2].
But I was going to try it through pointer arithmetic.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
int main(int argc, char *argv[])
{
if (argc > 4 || argc == 0) {
fprintf(stderr, "dice usage error\n");
exit(EXIT_FAILURE);
}
int c, d, x, y;
char **ptr = &argv[1];
x = strtol(argv[2], NULL, 10);
y = strtol(argv[3], NULL, 10);
if (*(ptr) || *(ptr + 1) == 'a') {
printf("%i\n", x + y);
exit(0);
}
di.c: In function `main':
di.c:16: warning: comparison between pointer and integer
di.c:19: error: syntax error at end of input
Errors from running gcc -c di.c
Bill
by argv[1]. Simple enough. But I am also wanting to add a switch, "-a" to be
able to run a routine that adds two numbers and returns the result. So I
want the program to accept "-a" or "a" or "a-" as the switch. Which would be
*(argv[1]) *(argv[1]+1) or *(argv[1]+2) that I would need C to examine. Or
argv[1][0] argv[1][1] argv[1][2].
But I was going to try it through pointer arithmetic.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
int main(int argc, char *argv[])
{
if (argc > 4 || argc == 0) {
fprintf(stderr, "dice usage error\n");
exit(EXIT_FAILURE);
}
int c, d, x, y;
char **ptr = &argv[1];
x = strtol(argv[2], NULL, 10);
y = strtol(argv[3], NULL, 10);
if (*(ptr) || *(ptr + 1) == 'a') {
printf("%i\n", x + y);
exit(0);
}
di.c: In function `main':
di.c:16: warning: comparison between pointer and integer
di.c:19: error: syntax error at end of input
Errors from running gcc -c di.c
Bill