我想邀请2个人加入这个俱乐部,所以有一个教父这样做,他必须邀请这2个人,但是第一个有优先邀请另外2个人的机会,然后第二个有机会这样做。但我不知道为什么它正在阅读第一个,但当我尝试邀请第二个时,程序停止了。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct people{
char name[40];
int id;
struct people *pgodFather;
struct people *pfirst;
struct people *psecond;
}People;
int id = 0; // Percorrer os arrays
bool accepted;
People *peopleList; // Apontar para o primeiro
int initialSize = 10;
People **arrayPeople;
void verificationSize(){
if(id >= initialSize){
initialSize *= 2;
arrayPeople = realloc(arrayPeople, initialSize * sizeof(People));
}
}
bool verificationId(){
int ids;
printf("Type your ID: ");
scanf("%d", &ids);
if(ids == ((2*(id-1)) + 1) || ((2*(id-1)) + 2)){
return true;
} else{
printf("NO PRIORITY\n");
return false;
}
}
void invite(){
system("cls");
verificationSize();
People *p = (People*)malloc(sizeof(People));
if(id == 0){
p->id = 0;
accepted = true;
}else if(id > 0){
accepted = verificationId();
} else{
printf("ID ERROR\n");
accepted = false;
}
if(accepted == true){
// arrayPeople[id] = p;
printf("Type your name: ");
scanf("%s", p->name);
p->pgodFather = peopleList; // null, [0], [1],
peopleList = p; // [0], [1], [2]
printf("Invite the first one: ");
scanf("%s", arrayPeople[(2*id) + 1]->name);
printf(arrayPeople[(2*id) + 1]->name);
p->pfirst = arrayPeople[(2*id) + 1];
char name[40];
printf("Invite the second one: ");
scanf("%s", arrayPeople[(id*2)+2]->name);
p->psecond = arrayPeople[(id*2) + 2]; // PROBLEMA
printf(arrayPeople[(2*id) + 2]->name);
id++;
}
}
int main(void){
arrayPeople = (People**) malloc(initialSize * sizeof(People*));
int choice;
bool stop = false;
while(!stop){
printf("CROWS CLUB\n");
printf(" 1 - Invite\n");
printf(" 2 - Search\n");
printf(" 3 - Display\n");
printf(" 4 - Delete\n");
printf(" 5 - Exit\n");
printf("Choose the alternative: ");
scanf("%d", &choice);
switch(choice){
case 1:
invite();
break;
case 2:
// search();
break;
case 3:
// display();
break;
case 4:
// delete();
break;
case 5:
stop = true;
break;
default:
printf("Invalid Option");
break;
}
}
return 0;
}
// for(int f = 0; f < initialSize; f++){
// free(arrayPeople[f]);
// }
// free(arrayPeople);
我想邀请遵循这个规则的人,我必须在这个数组中进行
您只分配了数组
arrayPeople
,并没有初始化其元素。arrayPeople[(2*id) + 1]
这样的元素具有 不确定的值,并且使用这些值(例如像 arrayPeople[(2*id) + 1]->name
那样取消引用)会调用 未定义的行为。在取消引用之前分配内存并分配给元素。
示例(为简单起见,省略了错误检查):
printf("Invite the first one: ");
arrayPeople[(2*id) + 1] = malloc(sizeof(**arrayPeople)); // allocate memory
scanf("%s", arrayPeople[(2*id) + 1]->name);
fputs(arrayPeople[(2*id) + 1]->name, stdout);
p->pfirst = arrayPeople[(2*id) + 1];
char name[40];
printf("Invite the second one: ");
arrayPeople[(id*2)+2] = malloc(sizeof(**arrayPeople)); // allocate memory
scanf("%s", arrayPeople[(id*2)+2]->name);
p->psecond = arrayPeople[(id*2) + 2]; // PROBLEMA
fputs(arrayPeople[(2*id) + 2]->name, stdout);
另请注意:
malloc()
家庭的选角结果被视为不好的做法。printf
的第一个参数是危险的,因为输入可能包含用于格式化的 %
。您应该使用 printf(something)
或 fputs(something, stdout)
,而不是 printf("%s", something)
。verificationSize
中的分配是错误的。数组 arrayPeople
的元素为 People*
,因此分配大小应为 initialSize * sizeof(People*)
或 initialSize * sizeof(*arrayPeople)
,而不是 initialSize * sizeof(People)
。ids == ((2*(id-1)) + 1) || ((2*(id-1)) + 2)
中的条件 verificationId
对于所有小到不会导致溢出的正 id
始终为真,因为 ((2*(id-1)) + 2)
始终为正。您可能想使用 ids == ((2*(id-1)) + 1) || ids == ((2*(id-1)) + 2)
来代替。