尝试打印结构时出现分段错误

时间:2019-11-16 16:37:22

标签: c struct

我正在尝试编写一个代码,该代码将允许用户输入他想要的任何数量的条目,然后将其打印出来(以及其他功能,但仍然必须达到此目的)。但是,当我尝试启动代码时,它允许我输入条目,但是当我要打印它们时,它不会注册current.namecurrent.telNo(仅打印出1: has tel. No. ),然后出现细分错误。知道我该如何解决。

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

int listSize;
int counter = 0;

struct Entry {
        char name[20];
        char telNo[9];
        struct Entry *next;
} current;

int main()
{
    struct Entry *linkedList = (struct Entry * )malloc(sizeof(struct Entry));
    struct Entry *current = linkedList;
    first(current);
    print(current);
    return 0;
}

void first(struct Entry linkedList)
{
    int i;
    printf("enter list size: ");
    scanf("%d", &listSize);
    printf("Now enter entries one by one: \n");
    for (i = 0; i < listSize; i++) {
        counter++;
        printf("Name: ");
        scanf("%s", linkedList.name);
        printf("Telephone: ");
        scanf("%s", linkedList.telNo);

        if (i != listSize -1) {
            linkedList.next = (struct Entry *)malloc(sizeof(struct Entry));
            linkedList = *linkedList.next;
        } else {
            linkedList.next = NULL;
        }
    }
}

void print(struct Entry linkedList)
{
    int nr = 1;
    printf("\nTelephone book is:\n");
    while (current.name != NULL) {
        printf("%d: %s has tel. No.\t%s\n", nr, current.name, current.telNo);
        current = *current.next;
        nr++;
    }
}     

4 个答案:

答案 0 :(得分:1)

代替。您应该使用-> ,并且在 print()中遍历了 current 而不是 linkedList 问题。同样,您的函数定义应先于其使用。请检查以下代码段,我已进行了相应的更改。

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

int listSize;
int counter = 0;

struct Entry {
    char name[20];
    char telNo[9];
    struct Entry *next;
} current;


void print(struct Entry *linkedList)
{
    int nr = 1;
    printf("\nTelephone book is:\n");
    while (linkedList->name != NULL) {
      printf("%d: %s has tel. No.\t%s\n", nr, linkedList->name, linkedList->telNo);
      linkedList = linkedList->next;
      nr++;
    }

} 
void first(struct Entry *linkedList)
{
    int i;
    printf("enter list size: ");
    scanf("%d", &listSize);
    printf("Now enter entries one by one: \n");
    for (i = 0; i < listSize; i++) {
        counter++;
        printf("Name: ");
        scanf("%s", linkedList->name);
        printf("Telephone: ");
        scanf("%s", linkedList->telNo);

        if (i != listSize -1) {
            linkedList->next = (struct Entry *)malloc(sizeof(struct Entry));
            linkedList = linkedList->next;
        } else {
            linkedList->next = NULL;
        }
    }
}

int main()
{
    struct Entry *linkedList = (struct Entry * )malloc(sizeof(struct Entry));
    struct Entry *current = linkedList;
    first(current);
    print(current);
    return 0;
}

答案 1 :(得分:0)

while (current.name != NULL) 

应该是

while (current != NULL)

否则current = *current.next将变为NULL并在while条件下崩溃

答案 2 :(得分:0)

您的代码中存在许多错误,但本质上是两个主要的“混淆点”:

首先,您似乎将结构(struct Entry)与指向结构的指针(例如,struct Entry *next;)混淆了。

第二,您有两个名为current的变量-一个全局定义的 (这是唯一的 ,即“ print函数可见”),另外一个在main内部定义了 本地 (此函数将“隐藏”前者)。

这是您的代码的更正版本,无论我在哪里进行更改,均带有三斜杠(///)注释。随时要求进一步的澄清和/或解释。

#include <stdio.h>
#include <stdlib.h>
int listSize;
int counter = 0;

struct Entry {
    char name[20];
    char telNo[9];
    struct Entry* next;
} *current; /// Here, we define a GLOBAL variable, "current" that is a POINTER to the struct

void first(struct Entry* linkedList); /// Put "Function Prototypes* here, so that "main" knows what they are
void print(struct Entry* linkedList); /// You need to pass POINTERS to structures, not the actual structures

int main()
{
    struct Entry* linkedList = (struct Entry*)malloc(sizeof(struct Entry));
//  struct Entry* current = linkedList; /// This declaration 'hides' the global variable
    current = linkedList; /// Here, we (properly) assign the global pointer's value

    first(current);
    print(current);
    return 0;
}

void first(struct Entry* linkedList) /// Pointer (see above)
{
    int i;
    printf("enter list size: ");
    scanf("%d", &listSize);

    printf("Now enter entries one by one: \n");
    for (i = 0; i < listSize; i++) {
        counter++;
        printf("Name: ");
        scanf("%s", linkedList->name); /// Use "->" instead of "." to get a pointer's member (and elsewhere)
        printf("Telephone: ");
        scanf("%s", linkedList->telNo); /// See above

        if (i != listSize - 1) {
            linkedList->next = (struct Entry*)malloc(sizeof(struct Entry)); /// See above
            linkedList = linkedList->next; /// Again - changed here to use pointers!
        }
        else {
            linkedList->next = NULL; /// See above
        }
    }
}

void print(struct Entry* linkedList)  /// Pointer (see above)
{
    int nr = 1;
    printf("\nTelephone book is:\n");
    while (current != NULL) { /// You need to check if "current" is not NULL before trying to access any of its members...
//  while (current.name != NULL) {
        printf("%d: %s has tel. No.\t%s\n", nr, current->name, current->telNo); /// Same "." to "->" changes as before
        current = current->next; /// And again!
        nr++;
    }
}

还有其他方法可以“修复”代码(并进行改进):例如,您永远不会使用传递给print的参数,而是依靠我提到的“全局”变量

答案 3 :(得分:0)

current.name是指向保留的20字节区域的指针,该区域始终为非NULL。 每次分配它时,我都会用NULL初始化* next(有几种方法可以做到这一点,最简单的方法是:在malloc之后立即为其分配NULL)。并执行如下检查:if (current.next != NULL)。 您还可以检查current.name[0] != 0,但第一个选择是更干净的。