我在类里面的一个项目上遇到了问题。我在递归打印 spheres
链表时遇到了特别大的麻烦。每当程序运行到特定部分时:
ss=ss->next;
存在段错误:11
。可能是什么问题?
(注意:我已经包含了必要的structs
sphere和
sphere_list,并省略了
rgb和
vec` 所以为了不弄乱代码。)
typedef struct sphere {
vec *center;
double radius;
rgb *color;
} sphere;
typedef struct sphere_list sphere_list;
/* convention: NULL is the empty sphere list */
struct sphere_list {
sphere *s;
sphere_list *next;
};
void sl_print(sphere_list *ss)
{
if(ss==NULL)
printf("SPHERE LIST EMPTY\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("SPHERE LIST:\n");
int i=1;
while(ss->s!=NULL){
printf("\t%d ", i);
sphere_print(ss->s);
if(ss->next==NULL){
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
return;
}
ss=ss->next;
i++;
}
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
return;
}
最佳答案
试试这个:
void sl_print(sphere_list *ss)
{
if(ss==NULL){
printf("SPHERE LIST EMPTY\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("SPHERE LIST:\n");
return ;
}
int i=1;
while(ss != NULL){
printf("\t%d ", i);
sphere_print(ss->s);
ss=ss->next;
i++;
}
}
关于c - 链表 while 循环的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21935502/