发送列表指向函数

时间:2016-12-07 05:23:17

标签: c function pointers

对于我正在做的关于C中指针的练习,我希望通过将指针listStart与我想要插入的结构一起发送到insertEntry函数,在链表的开头插入一个struct。但是,使用这个当前代码我无法做到这一点,因为listStart指针不带有它在main函数中指向的地址(列表的第一个结构)。

我知道我只是将指针本身复制到insertEntry函数中,因此它所指向的地址被省略了。这意味着我在insertEntry函数中得到的listStart指针是一个空指针。

为了解决这个问题,我尝试将listStart作为指针发送到insertEntry函数,但是,它只是给了一个指向指向null的指针。我试图将listStart的地址发送到不起作用的函数,因为它向函数发送了null。

我有的问题:是否有可能做到这一点,我只是错过了什么?或者这不可能吗?

先谢谢。

// header to include standard input and output
#include <stdio.h>

struct entry
{
    int value;
    struct entry *next;
};


// prototype for insertEntry function
void insertEntry(struct entry *l, struct entry *i, struct entry *j);

int main(void)
{

    // declaration of array for linked list
    struct entry list1 = { 1 }, list2 = { 2 }, list3 = { 3 }, list4 = { 4 }, list5 = { 5 }, insert = { 8 }; 
    struct entry *listStart = &list1;

    // test to see if the value of the insert.value struct is correct
    printf("insert.value = %i\n", insert.value);


    // assign pointers in list.next to the next struct in the list to create a linked list
    list1.next = &list2;
    list2.next = &list3;
    list3.next = &list4;
    list4.next = &list5;
    list5.next = (struct entry *) 0;

    // print the linked list to make sure the pointers are going to the correct struct member
    printf("Original list!\n");
    while ( listStart != (struct entry *) 0 ) 
    {
        printf ("%i\n", listStart->value);
        listStart = listStart->next;
    }

    // send struct to change and struct to insert
    insertEntry(listStart, &insert, &list1);

    // restart the list from the beginning because in the last while loop the listStart was assigned to the null pointer.
    listStart  = &list1;

    // print the new list to show what has been inserted and moved around
    printf("New list!\n");
    while ( listStart != (struct entry *) 0 ) 
    {
        printf ("%i\n", listStart->value);
        listStart = listStart->next;
    }

    return 0;
}

// function to insert a new struct in the list and redirect an old struct in the list
void insertEntry(struct entry *l, struct entry *i, struct entry *j)
{
    i->next = l; // this is assigning the mem add of the pointer is list2.next to that of insert.next
    l = i; // this is assigning the mem add of i which would be insert.value to the pointer in list2.next

}

4 个答案:

答案 0 :(得分:0)

以下是您将如何完成任务的示例:

void insertEntry(struct entry** pplistStart, struct entry *pNewEntry)
{
     pNewEntry->next = *pplistStart;
     *pplistStart = pNewEntry;
}

可以像

一样调用
 insertEntry(&listStart, insert);

答案 1 :(得分:0)

问题1

您正在更改第一个listStart循环中while指向的位置。在该循环结束时,listStart的值为NULL

我建议您使用另一个变量来迭代列表中的项目,并使listStart指向列表的开头。

struct entry *iter = listStart;
while ( iter != NULL ) 
{
    printf ("%i\n", iter->value);
    iter = iter->next;
}

在两个循环中使用iter以使listStart指向列表的开头。

问题2

致电insertEntry后,listStart未指向列表的开头。 list1也不是列表中的第一个条目。 insert是列表开头的对象。替换

listStart  = &list1;

通过

listStart  = &insert;

建议清理

该行

l = i;
insertEntry中的

无用。它在函数中没有任何作用。它也不会改变main中的任何内容。删除它。

答案 2 :(得分:0)

你有两个问题: -

一个是: -

printf("Original list!\n");
while ( listStart->next != (struct entry *) 0 )
{
    printf ("%i\n", listStart->value);
    listStart = listStart->next;
}

// send struct to change and struct to insert
insertEntry(listStart, &insert, &list1);
insertEntry()中的

listStart现在指向NULL,所以当你调用insertEntry()时,你会得到段错误。

第二个问题是: -

你在i-&gt;中传递l的地址的逻辑是错误的。你应该在l-&gt; next中指定i的地址。

void insertEntry(struct entry *l, struct entry *i, struct entry *j)
{
    i->next = l; // this is assigning the mem add of the pointer is list2.next to that of insert.next
    l = i; // this is assigning the mem add of i which would be insert.value to the pointer in list2.next

}

我修改了你的程序看看。

// header to include standard input and output
#include <stdio.h>

struct entry
{
    int value;
    struct entry *next;
};


// prototype for insertEntry function
void insertEntry(struct entry *l, struct entry *i, struct entry *j);

int main(void)
{

    // declaration of array for linked list
    struct entry list1 = { 1 }, list2 = { 2 }, list3 = { 3 }, list4 = { 4 }, list5 = { 5 }, insert = { 8 };
    struct entry *listStart = &list1;

    // test to see if the value of the insert.value struct is correct
    printf("insert.value = %i\n", insert.value);

    // assign pointers in list.next to the next struct in the list to create a linked list
    list1.next = &list2;
    list2.next = &list3;
    list3.next = &list4;
    list4.next = &list5;
    list5.next = (struct entry *) 0;

    // print the linked list to make sure the pointers are going to the correct struct member
    printf("Original list!\n");
    while ( listStart->next != (struct entry *) 0 )
    {
        printf ("%i\n", listStart->value);
        listStart = listStart->next;
    }

    // send struct to change and struct to insert
    insertEntry(listStart, &insert, &list1);

    // restart the list from the beginning because in the last while loop the listStart was assigned to the null pointer.
    listStart  = &list1;

    // print the new list to show what has been inserted and moved around
    printf("New list!\n");
    while ( listStart != (struct entry *) 0 )
    {
        printf ("%i\n", listStart->value);
        listStart = listStart->next;
    }

    return 0;
}

// function to insert a new struct in the list and redirect an old struct in the list
void insertEntry(struct entry *l, struct entry *i, struct entry *j)
{
    l->next = i; // this is assigning the mem add of the pointer is list2.next to that of insert.next
    l = i; // this is assigning the mem add of i which would be insert.value to the pointer in list2.next

}

答案 3 :(得分:0)

执行while ( listStart != (struct entry *) 0 )循环后,listStart指向NULL,而insertEntry内,您正在为l next分配插入istStart指针这是NULL

您可以尝试以下两种解决方案,

  1. 内部insertEntry更改
  2. i->next = l

    i->next = j

    1. 在致电insertEntrylistStart指向list1
    2. 之前

      listStart = &list1; insertEntry(listStart, &insert, &list1);