【发布时间】:2018-01-16 05:39:11
【问题描述】:
我想将一个结构指针传递给一个函数,该函数将在传递的结构指针指向的位置动态创建一个结构数组。我能够成功地创建和填充结构数组,但是当尝试使用传递的指针在调用函数中打印数据时,会给我一个垃圾值。请帮助我知道为什么我的结构指针指向垃圾以及如何正确访问我的数据。
以下只是一些示例代码,用于演示如何使用 malloc 和 realloc 传递和动态填充结构。这是不正确的方法:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student *record);
int main()
{
struct student *record = NULL;
record = (struct student *)malloc(sizeof(struct student));
func(record);
if(record != NULL)
{
for(int i=0; i<2; i++)
{
printf(" 1 Id is: %d \n", record[i].id);
printf(" 1 Name is: %s \n", record[i].name);
printf(" 1 Percentage is: %f \n", record[i].percentage);
printf("\n");
}
}
else
{
printf("record pointer is null");
}
return 0;
}
void func(struct student *record1)
{
for(int i=0; i<2; i++)
{
if(i)
{
record1 = (struct student *)realloc(record1,sizeof(struct student)*(i+1));
}
record1[i].id=1;
strcpy(record1[i].name, "Raju");
record1[i].percentage = 86.5;
}
}
以下是使用双指针的类似示例,这是执行此操作的正确方法:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student **record);
int main()
{
struct student *record = NULL;
func(&record);
if(record != NULL)
{
for(int i=0; i<2; i++)
{
printf(" 1 Id is: %d \n", record[i].id);
printf(" 1 Name is: %s \n", record[i].name);
printf(" 1 Percentage is: %f \n", record[i].percentage);
printf("\n");
}
}
else
{
printf("record pointer is null");
}
free(record);
return 0;
}
void func(struct student **record1)
{
*record1 = (struct student *)malloc(sizeof(struct student));
for(int i=0; i<2; i++)
{
if(i)
{
*record1 = (struct student *)realloc(*record1,sizeof(struct student)*(i+1));
}
(*record1)[i].id=1;
strcpy((*record1)[i].name, "Raju");
(*record1)[i].percentage = 86.5;
}
}
【问题讨论】:
-
如果您使用 C++ 编程,为什么要使用
malloc和free和realloc?这里的“正确”解决方案是使用std::vector<TEST_STRUCT>。 -
@Someprogrammerdude 我主要尝试基于 C 的解决方案,但如果需要我也可以使用 C++ 概念,我会研究向量。
-
@O'Neil 由于我发布的示例存在很多问题,因此我将其替换为新的更简单的示例,其中一个正在运行。因此,既然您提到 ap_test 是我的指针测试的副本,那么如果我需要它指向与原始指针相同的位置,我应该怎么做。我曾尝试使用双指针,但我收到调试断言失败消息。令人惊讶的是,在我提供的新示例中,它甚至可以使用指针的副本。
-
record1=>*record1在realloc内。使用临时指针来做所有事情并最终分配*record1 = that_pointer;不太容易出错。关于令人惊讶的复制结果,我猜 realloc 在重新分配时没有更改地址。 -
@O'Neil 确保在您的文章中解释为什么传递
struct student *record不起作用。record的地址在main和func之间是如何分开和不同的,以及为什么需要传递record的地址。
标签: c arrays pointers data-structures