. 이전에 동적으로 함수에 할당 된 배열에 액세스하려고하자마자 프로그램이 중단되었습니다. 여기에 몇 가지 코드 내 main
C에서 함수에서 동적으로 할당 된 배열에 액세스하는 프로그램이 충돌했습니다.이 시간을내어 주셔서 감사합니다.
Player * array=NULL;
//I'm passing nPlayers because i want the value to be saved and available on my main
setup(array, &nPlayers);
for (i=0; i<nPlayers; i++){
printf("It's %s 's turn\n", (array)[i].playerName);
dices=diceRoll(&same);
}
에서
//function to allocate my array, gives an array as return
Player* allocate(Player **a, int n) {
*a = (Player *) malloc(sizeof(Player)*(n));
return *a;
}
//populating my allocated array, return an array
Player* initializePlayers(Player *a, int n){
int i=0;
char tmp[MAXCHAR];
for(i=0; i<n; i++){
printf("Insert player name %d\n", i);
scanf("%s", tmp);
strcpy(a[i].playerName,tmp);
printf("Player %s assigned.\n", a[i].playerName);
a[i].playerNumber=i;
}
return a;
}
//setup function which includes both the above ones, called from main
void setup(Player *array, int *nPlayers){
int done=0;
while (done==0){
printf("How many players?\n");
scanf("%d", nPlayers);
if (*nPlayers <2 || *nPlayers>8){
printf("Choose between 2 and 8\n");
waitFor(2);
clear();
done=0;
}
else done=1;
}
allocate(&array, *nPlayers);
initializePlayers(array, *nPlayers);
}
그래서 나는 꽤 분명 뭔가를 누락 될 수 있습니다에 대한 아무것도하지 마십시오 프로그래밍 비교적 새로운 내가있어입니다 부여
A [, 최소 완료하고 검증 가능한 예]를 작성하십시오 (HTTP : // 유래
귀하의
setup()
은 다음과 같이해야한다 .com/help/mcve)에'Player'의 선언을 포함하여 필요한 모든 선언과 정의가 포함되어 있어야합니다. – MikeCAT