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

标签 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++;
    }
}     

最佳答案

而不是 .您应该使用 -> 并且在您的 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;
}

关于c - 尝试打印结构时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892889/

相关文章:

arrays - 更改数组中结构的值

swift - 在 Swift 3 中访问结构之外的值

c - 如果我使用超过 25 个元素的数组,msp430g2211 (launchpad) 在启动时停止

C 中函数前的 const 关键字

c - 当IP指向0时调试指令指针

c - 返回一个结构体类型

c - 是否可以将指针从结构类型转换为扩展 C 中第一个结构类型的另一种结构类型?

c - 类型转换浮点值或使用 math.h floor* 函数?

C代码int到字符串不起作用

c - 有没有办法在函数中使用 typedef 结构变量...?