C语言编译报错:Undeclared identifier 'parent0'问题求助
血型遗传模拟C语言代码编译错误排查
问题描述
编写的血型遗传模拟C代码编译时出现错误:
inheritance.c:52:17: error: use of undeclared identifier 'parent0'
错误出现在create_family函数的if (generations > 1)代码块内,尝试修改parent0的变量名后问题依旧,无法理解错误原因,需要排查。
错误根源分析
核心问题是变量与类型命名冲突,同时伴随结构体成员名拼写错误:
- 在
create_family函数中,新分配的结构体指针被命名为person,和typedef定义的结构体类型名person完全重名。编译器会优先把person识别为类型名,而非定义的指针变量,导致后续person->parent[0]这类操作不符合语法,进而引发parent0未声明的误报(本质是编译器无法正确解析指针变量)。 - 结构体定义中存储父母的成员是
parents[2](复数),但代码中全程写成了parent[0]/parent[1](单数),编译器找不到对应的结构体成员。 free_family函数的基例判断错误:直接访问p->parent[0]会在p为空指针时触发崩溃,应该先判断p == NULL。create_family函数末尾有多余的return NULL,永远不会被执行,属于无效代码。
修复步骤
- 将
create_family中分配的指针变量名从person *person改为person *new_person,避免和类型名冲突; - 把所有
person->parent替换为new_person->parents,匹配结构体定义的成员名; - 修正
free_family的基例判断逻辑,先检查p是否为空; - 删除
create_family末尾多余的return NULL语句。
修复后的完整代码
// Simulate genetic inheritance of blood type #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <time.h> // Each person has two parents and two alleles typedef struct person { struct person *parents[2]; char alleles[2]; } person; const int GENERATIONS = 3; const int INDENT_LENGTH = 4; person *create_family(int generations); void print_family(person *p, int generation); void free_family(person *p); char random_allele(); int main(void) { // Seed random number generator srand(time(0)); // Create a new family with three generations person *p = create_family(GENERATIONS); // Print family tree of blood types print_family(p, 0); // Free memory free_family(p); } // Create a new individual with `generations` person *create_family(int generations) { // Allocate memory for new person person *new_person = malloc(sizeof(person)); if (new_person == NULL) { return NULL; } // If there are still generations left to create if (generations > 1) { // Create two new parents for current person by recursively calling create_family person *parent0 = create_family(generations - 1); person *parent1 = create_family(generations - 1); // Set parent pointers for current person new_person->parents[0] = parent0; new_person->parents[1] = parent1; // Randomly assign current person's alleles based on the alleles of their parents if (rand() % 2 == 0) { new_person->alleles[0] = new_person->parents[0]->alleles[0]; } else { new_person->alleles[0] = new_person->parents[0]->alleles[1]; } if (rand() % 2 == 0) { new_person->alleles[1] = new_person->parents[1]->alleles[0]; } else { new_person->alleles[1] = new_person->parents[1]->alleles[1]; } } // If there are no generations left to create else { // Set parent pointers to NULL new_person->parents[0] = NULL; new_person->parents[1] = NULL; // Randomly assign alleles new_person->alleles[0] = random_allele(); new_person->alleles[1] = random_allele(); } // Return newly created person return new_person; } // Free `p` and all ancestors of `p`. void free_family(person *p) { // Handle base case: if p is NULL, do nothing if (p == NULL) { return; } // Free parents recursively free_family(p->parents[0]); free_family(p->parents[1]); // Free child free(p); } // Print each family member and their alleles. void print_family(person *p, int generation) { // Handle base case if (p == NULL) { return; } // Print indentation for (int i = 0; i < generation * INDENT_LENGTH; i++) { printf(" "); } // Print person if (generation == 0) { printf("Child (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]); } else if (generation == 1) { printf("Parent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]); } else { for (int i = 0; i < generation - 2; i++) { printf("Great-"); } printf("Grandparent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]); } // Print parents of current generation print_family(p->parents[0], generation + 1); print_family(p->parents[1], generation + 1); } // Randomly chooses a blood type allele. char random_allele() { int r = rand() % 3; if (r == 0) { return 'A'; } else if (r == 1) { return 'B'; } else { return 'O'; } }
内容的提问来源于stack exchange,提问作者Santiago De Luca
相关产品推荐
相关产品推荐

