从填充功能传递指向链表的指针不起作用

时间:2011-07-26 11:24:35

标签: c

我正在四处寻找指针和结构。我想实现以下目标: (1)定义一个带结构的链表(numberRecord) (2)编写一个函数,通过循环(fillList)填充带有一些样本记录的链表 (3)计算链表中的元素数 (4)打印元素数量

我现在到目前为止,fillList函数运行良好,但我没有成功将填充的链表移交给main()中的指针。在下面的代码中,printList函数只显示在main()中添加的单个记录,而不是显示在函数fillList中创建的列表。


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

typedef struct numberRecord numberRecord;

//linked list
struct numberRecord {
             int number;
        struct numberRecord *next;
};

//count #records in linked list
int countList(struct numberRecord *record) {

         struct numberRecord *index = record;
    int i = 0;

    if (record == NULL)
        return i;

    while (index->next != NULL) {
        ++i;
        index = index->next;
    }

    return i + 1;
}

//print linked list
void printList (struct numberRecord *record) {

    struct numberRecord *index = record;

    if (index == NULL)
        printf("List is empty \n");

    while (index != NULL) {

        printf("%i \n", index->number);
        index = index->next;
    }

}

//fill the linked list with some sample records
void fillList(numberRecord *record) {

    numberRecord *first, *prev, *new, *buffer;

//as soon as you add more records you get an memory error, static construction
    new = (numberRecord *)malloc(100 * sizeof(numberRecord));
    new->number = 0;
    new->next = NULL;

    first = new;
    prev = new;
    buffer = new;

    int i;

    for (i = 1; i < 11; i++) {

        new++;

        new->number = i;
        new->next = NULL;

        prev->next = new;
        prev = prev->next;
    }

    record = first;
}


int main(void) {

    numberRecord *list;
    list = malloc(sizeof(numberRecord));
    list->number = 1;
    list->next = NULL;

    fillList(list);
    printf("ListCount: %i \n", countList(list));
    printList(list);
    return 0;
}

解 请阅读下面的帖子,他们指出了这个解决方案,并包含一些关于指针的非常有见地的评论。适用的代码下方:

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

typedef struct numberRecord numberRecord;

//linked list
struct numberRecord {
             int number;
        struct numberRecord *next;
};

//count #records in linked list
int countList(struct numberRecord *record) {

         struct numberRecord *index = record;
    int i = 0;

    if (record == NULL)
        return i;

    while (index->next != NULL) {
        ++i;
        index = index->next;
    }

    return i + 1;
}

//print linked list
void printList (struct numberRecord *record) {

    struct numberRecord *index = record;

    if (index == NULL)
        printf("List is empty \n");

    while (index != NULL) {

        printf("%i \n", index->number);
        index = index->next;
    }

}

//fill the linked list with some sample records
 numberRecord *fillList() {

    numberRecord *firstRec, *prevRec, *newRec;

    int i;

    for (i = 1; i < 11; i++) {

        newRec = malloc(sizeof(numberRecord));
        newRec->number = i;
        newRec->next = NULL;

        //initialize firstRec and prevRec with newRec, firstRec remains head
        if (i == 1) {
            firstRec = newRec;
            prevRec = newRec;
        }
        prevRec->next = newRec;
        prevRec = prevRec->next;
    }

    return firstRec;
}


int main(void) {

    numberRecord *list;
    list = fillList();

    printf("ListCount: %i \n", countList(list));
    printList(list);
    return 0;
}

1 个答案:

答案 0 :(得分:2)

fillList

中的此声明
record = first;

list中的main变量没有影响。指针在C中按值传递(与其他所有内容一样)。如果您要更新list中的main变量,则必须传递指向它的指针(&list)并相应地修改fillList,或者返回{来自numberRecord*的{​​1}}。 (我实际上是第二种选择。)

这是一个(坏)插图:

fillList调用fillList时,在该函数的起始点,指针是这样的:

main

稍后在main memory fillList list ----> 0x01234 <---- record 中,你为fillList分配了一些存储空间(这实际上是一个坏名称,它与C ++中的运算符冲突,会让人感到困惑)

new

main memory fillList list ----> 0x01234 <---- record 0x03123 <---- new 的最后一行,您将离开:

fillList

main memory fillList list ----> 0x01234 ,-- record 0x03123 <---- new record不是同一个变量。它们以相同的值开头,但更改list不会更改record。它们都是指针的事实并没有使它们与list在这方面有任何不同。

您可以在int中按列表更改指向的内容,但您无法更改fillList 指向的内容(代码版本。)

最简单的解决方法是更改​​list,如下所示:

fillList

numberRecord *fillList() { .... return new; } 中,不要直接分配main,只需致电list进行初始化。