【问题标题】:Structure Link List Segmentation-Fault结构链表分段-故障
【发布时间】:2012-09-30 23:13:51
【问题描述】:

我刚开始在我的实验室中计算课程的 GPA,这些课程的信息存储在结构的链接列表中。到目前为止,我正在尝试打印所有课程信息,以确保它们已正确初始化并添加到链接列表中。

我遇到了一个问题,因为我不断收到分段错误。我理解分段错误的含义,但我不知道我在哪里犯了错误。任何帮助,将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAX_CLASSES 20


/* Function Prototypes */
struct course * initcourse( int, char *, char *, float, char *, char *);
void add( struct course * );

/* Definition of a data node holding course information */
  struct course {
    int term;
    char name[15];
    char abbrev[20];
    float hours;
    char grade [4];
    char type[12];
    struct course *next;
  };


/* head points to first node in list, end points to last node in list */
/* initializes both to NULL, no nodes yet */
struct course *head = (struct course *) NULL;
struct course *end = (struct course *) NULL;


/* Initializes a node, allocates memory for the node, and returns     */
/* a pointer to the new node. Must pass correct parameters.           */
struct course * initcourse( int term, char *name, char *abbrev, float hours, char *grade, char *type)
{
  struct course *ptr;
  ptr = (struct course *) calloc( 1, sizeof(struct course ) );
  if( ptr == NULL )

    return (struct course *) NULL;

  else
    {
      ptr->term = term;
      strcpy( ptr->name, name );
      strcpy( ptr->abbrev, abbrev );
      ptr->hours = hours;
      strcpy( ptr->grade, grade );
      strcpy( ptr->type, type );
      return ptr;
    }
}


/* This adds a node to the end of the list. You must allocate a node and */
/* then pass its address to this function                                */ 
void add(struct course *new)
{
  if (head == NULL)
    {
      head = new;
    }
  else
    {
      end->next = new;
      end = new;
    }
}

/* Prints all information in a node */
void printnode( struct course *ptr )
{
  printf("Term ->%d\n", ptr->term );
  printf("Name ->%s\n", ptr->name );
  printf("Abbreviation ->%s\n", ptr->abbrev );
  printf("Hours ->%f\n", ptr->hours );
  printf("Grade ->%s\n", ptr->grade );
  printf("Type ->%s\n", ptr->type );
}




/* Prints List of Nodes */
void printlist( struct course *ptr ) 
{ 
  while( ptr != NULL ) 
    { 
      printnode( ptr ); 
      ptr = ptr->next;  
    } 
} 

/* Calculates GPA */
/* float gpa ( struct course *ptr ) */
/* { */
/*   float totalhours; */
/*   float gpa; */
/*   float gradepoints; */

/*   while (ptr != NULL ) */
/*     { */
/*       totalhours += (ptr->hours); */
/*       gradepoints = (ptr->hours * ptr->grade); */
/*     } */
/*   gpa = (gradepoints /ptr->hours); */
/* } */



int main()
{

  int term;
  char name[15];
  char abbrev[20];
  float hours;
  char grade[4];
  char type[12];
  float gpa;
  struct course *ptr;

  struct course course1, course2, course3;

  course1.term = 1234;
  strcpy(course1.name,"cse1234");
  strcpy(course1.abbrev,"systems");
  course1.hours = 4;
  strcpy(course1.grade,"A");
  strcpy(course1.type,"GEC");


  ptr = initcourse(course1.term, course1.name, course1.abbrev, course1.hours, course1.grade, course1.type);

  struct course *head, *ptr2;
  head = ptr;
  // ptr2 = ptr;

  add(ptr);

  course2.term = 4332;
  strcpy(course2.name,"cse4332");
  strcpy(course2.abbrev,"Database");
  course2.hours = 4;
  strcpy(course2.grade,"B");
  strcpy(course2.type,"Technical");

  ptr2 =  initcourse(course2.term, course2.name, course2.abbrev, course2.hours, course2.grade, course2.type);

  add(ptr2);

  printlist(head);



}

【问题讨论】:

  • 段错误 = 使用调试器!

标签: c pointers segmentation-fault structure


【解决方案1】:
void add(struct course *new)
{
  if (head == NULL)
    {
      head = new;
    }
  else
    {
      end->next = new;
      end = new;
    }
}

您需要在插入第一个节点时将end 设置为new(当head == NULL 时),否则在添加更多节点时会取消引用空指针。

initcourse 中,您应该将next 成员设置为NULL,因为标准不保证all-bits-0 是空指针表示(它非常 可能是,但不能保证)。

还有,

struct course *head, *ptr2;
head = ptr;

声明一个新的局部变量head,它会影响全局变量,而不是直接分配给head(即使它是错误的),您应该调用add(ptr);

【讨论】:

  • 谢谢您,这解决了问题。我还有一个小问题(不确定我是否应该创建一个新帖子)。我正在尝试用适当的成绩点定义每个字母等级。其中 A = 4.0 B = 3.0 我知道使用 #define 语句可以实现这一点,但我无法定义 +/- 。例如,“A-”应该 = 3.7。有没有合适的方法来定义所有这些字母等级? “#define”指令是处理此任务的正确方法吗?感谢您的帮助,如果这是不正确的地方,我深表歉意。 -马特
  • 您不能#define 任何包含“+”或“-”的名称,因此无论如何您都需要采用另一条路径。我不确定最好的方法是什么,你有 A 到 F 的等级,每个等级都有可能的 + 或 -?然后在第一个char 上加上一个switch 并根据第二个进行更正可能是一种方法。
【解决方案2】:

你没有初始化next指针,所以最后一个元素包含一个坏指针,指向一些垃圾。

你也不初始化end

另一个问题(与崩溃无关)是这段代码会创建一个重复的条目:

head = ptr;
add(ptr);

还有其他问题。你真的应该得到一个调试器,看看发生了什么。

【讨论】:

    猜你喜欢
    • 2014-01-29
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多