关于定义结构体指针数组
    typedef int score;
    typedef struct tnode *ptrtonode;
    typedef ptrtonode tree; 
    struct tnode{
         score s;
         tree next;
         bool know;
    }; 
    scanf("%d",&n);
    tree t[n];
    for(i=0;i<n;i++){
        scanf("%d",&x);
        t[i]->s=x;
      t[i]->next=NULL;
      t[i]->know=false;
  }
此声明仅保留内存空间。由于n没有在编译时定义,数组的内容包含垃圾值。
  tree t[n];
任何访问都t[i]将导致程序访问内存位置,用存在于其中的值表示t[i],很可能是无效内存位置的地址。这会导致你的崩溃。在其他情况下,它可能会导致程序无关部分的内存损坏,并产生不正确的结果或再次崩溃。
需要在分配节点之前初始化数组元素。
for(i=0;i<n;i++){
    scanf("%d",&x);
    t[i] = (struct tnode *)malloc(sizeof(tnode)); // this was missing.
    t[i]->s=x;
    t[i]->next=NULL;
    t[i]->know=false;
}
 
                    
                
                
            
        
浙公网安备 33010602011771号