我在理解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;
}
答案 0 :(得分:2)
这不仅仅是教授的要求,也是您的申请所要求的。您想要分配内存,并设置一个指针在外部指向该内存。所以很自然,你需要引用那个指针。 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
}
}
答案 1 :(得分: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
并且没有任何形式的迭代。< / p>
您可以阅读this link作为结构及其内存分配的参考。