为 'struct ** record' 的属性赋值

Assigning values to properties of a 'struct ** record'

我在理解 C 双指针概念时遇到了问题。本质上,我正在尝试编写代码来检查 record_1 是否尚未设置。如果没有,请设置它。如果已设置,我们将添加一个新的 struct record newRecord 到第一个记录 .next 指针。

教授要求我用双指针

我试过使用 firstRecord = malloc(sizeof(struct record*)); 但没有任何运气,也尝试取消引用 firstRecord.

遍历 addRecord 函数中的记录的 while 循环也没有按预期工作,因为我不知道如何处理双指针。

struct record
{
    int                accountno;
    char               name[25];
    char               address[80];
    struct record*     next;
};

int addRecord (struct record ** firstRecord, int accountno, char name[], char address[])
{
    if (firstRecord == NULL)
    {
        // Segmentation Fault here
        // (*firstRecord)->accountno = accountno;
        // Assign the name to the newRecord
        // strcpy((*firstRecord)->name, name);
        // Assign the name to the newRecord
        // strcpy((*firstRecord)->address, address);
        // Initialize the next record to NULL
        // (*firstRecord)->next = NULL;
    }
    else
    {
        // Define a new struct record pointer named newRecord
        struct record newRecord;
        // Assign the accountno of newRecord
        newRecord.accountno = accountno;
        // Assign the name to the newRecord
        strcpy(newRecord.name, name);
        // Assign the address to the newRecord
        strcpy(newRecord.address, address);
        // Initialize the next record to NULL
        newRecord.next = NULL;
        // Create a new record and add it to the end of the database
        struct record ** iterator = firstRecord;
        // Iterate through the records until we reach the end
        while (iterator != NULL)
        {
            // Advance to the next record
            *iterator = (*iterator)->next;
        }
        // Assign the address of newRecord to the iterator.next property
        (*iterator)->next = &newRecord;
    }

    return 1;
}

int main() {
    struct record ** firstRecord;
    firstRecord = NULL;

    addRecord(firstRecord, 1, "Foo", "Bar");
    addRecord(firstRecord, 2, "Foo", "Bar");

    return 0;
}

你不需要双指针,一个简单的指针就足够了。首先你需要为新记录动态分配内存然后给它设置值:

int main() {
    struct record *record1;
    record1 = NULL;

    if (record1 == NULL)
    {
        printf("\nrecord_1 is NULL\n");


        //dynamically allocate memory for record1
        record1 = malloc(sizeof(struct record);
        if (record_1 == NULL)
        {
            printf("Error allocating memory\n");
            return -1;
        }

        //set values to record1
        record1->accountno = ...;
        strcpy(record1->name, ...);
        strcpy(record1->address, ...);
        record1->next = NULL;
        return 0;
    }
    else
    {
        //do the same for record1->next
        record1->next = malloc(sizeof(struct record);
        if (record1->next == NULL)
        {
            printf("Error allocating memory\n");
            return -1;
        }

        //set values to record1
        record1->next->accountno = ...;
        strcpy(record1->next->name, ...);
        strcpy(record1->next->address, ...);
        record1->next->next = NULL;
        return 0;
    }
}

但是请注意,在这个程序的编写方式中,else 部分将永远不会到达,因为 record1 总是初始化为 NULL 并且没有迭代形式.

您可以阅读 this link 作为结构及其内存分配的参考。

不仅教授需要,您的申请也需要。您想要分配内存,并设置一个在函数 外部 定义的指针指向该内存。所以很自然地,您需要引用该指针。 C 允许您通过指向指针的指针来做到这一点。

那么您希望您的调用代码如下所示:

struct record * firstRecord = NULL;
addRecord(&firstRecord, 1, "Foo", "Bar");
addRecord(&firstRecord, 2, "Foo", "Bar");

您传递常规指针的地址,以便addRecord可以写入它。它通过取消引用它的参数来做到这一点,就像这样:

int addRecord (struct record ** pFirstRecord, /* ... */ )
{
    if (pFirstRecord == NULL)
      return 0; // We weren't passed a valid address of a pointer to modify

    if(*pFirstRecord == NULL)
    {
      // Here we check if the pointed to pointer already points to anything.
      // If it doesn't, then proceed with adding the first record
    }
}