I'm creating a AI player for a simple card game.
There will be an input by scanf of the number of legal cards. Then I want to create an array of that size.
However I have added an if statement to prevent errors if the user input is 0. (Because you cannot create an array point to something of size 0.)
My problem is... because I have created it in a if statement, I cannot access it outside of the if statement. This is shown by the warning "Use of undeclared identifier 'legal_cards'" when I try compile with Xcode on my Mac.
I'd love advice on how to better code this program. (I still need to use scanf to get input, but maybe there is a better way o
Is the only way to have all my code that relates to the array in the if statement? Or can I use it later (with another if statement checking to make sure (n_legal_cards > 0)
?
#include <stdio.h>
int main (void) {
int n_legal_cards;
printf("How many legal cards are there?\n");
scanf("%d", &n_legal_cards);
if (n_legal_cards > 0) {
int legal_cards[n_legal_cards];
}
/*
...
[scanning other variables in here]
[a little bit of other code here]
...
*/
if (n_legal_cards > 0) {
int rounds_suit = legal_cards[0];
}
return 0;
}