I want to scan numbers in from within a function, but have access to them in main, so I tried using pointers to do so:
Code:
// Path Of Exile socket colours simulation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 0==0
#define FALSE 0==1
#define RED 0
#define GREEN 1
#define BLUE 2
#define ALL 3
int isNum(char *s);
int scanSockets(int *colourP[ALL], int sockets);
int main(int argc, char **argv) {
if (argc != 2 || isNum(argv[1]) == FALSE) {
fprintf(stderr, "Format: ./poeChrom <num simulations>\n");
return 1;
}
int sockets;
int colour[ALL] = {0};
int *colourP[ALL] = {&colour[RED], &colour[GREEN], &colour[BLUE]};
int flag = FALSE;
printf("\n\tNumber of linked sockets: ");
scanf("%d", &sockets);
printf("\t-----\n\tSockets that are wished for\n"
"\t(format \"<R num> <G num> <B num>\" in that order)\n"
"\te.g. 1 2 2 for 1R 2G 2B: ");
if (scanSockets(colourP[ALL], sockets) == FALSE) {
fprintf(stderr, "Invalid entry\n\n");
return EXIT_FAILURE;
} else
printf("SUCCESS\n");
return EXIT_SUCCESS;;
}
int isNum(char *s) {
for (; *s != '\0'; s++)
if (*s < '0' || *s > '9') return FALSE;
return TRUE;
}
int scanSockets(int *colourP[ALL], int sockets) {
int colour[ALL];
int i, scan, valid = TRUE;
int sum = 0;
scan = scanf("%d %d %d", colour[RED], colour[GREEN], colour[BLUE]);
if (scan != 3)
return FALSE;
for (i=0; i<ALL; i++) {
if (colour[i] < 0) valid = FALSE;
sum += colour[i];
}
if (sum == 0 || sum > sockets) valid = FALSE;
for (i=0; i<ALL; i++) *colourP[i] = colour[i];
//printf("IN FUNC = %d %d %d\n", colour[RED], colour[GREEN]
// , colour[BLUE]);
return valid;
}
But I'm getting the errors
poeChrom.c: In function ‘main’:
poeChrom.c:34:5: warning: passing argument 1 of ‘scanSockets’ from incompatible pointer type [enabled by default]
poeChrom.c:15:5: note: expected ‘int **’ but argument is of type ‘int *’
poeChrom.c: In function ‘scanSockets’:
poeChrom.c:56:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
poeChrom.c:56:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]
poeChrom.c:56:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat]
If you can't follow what I'm trying to do, just ask and I'll try my best to explain it. Thanks!