C. 正确处理指向 mallocs 结构的函数的双指针和指向它的 returns 指针——在不同的函数中读取、显示、释放
C. Properly handling double pointers to function that mallocs a structure and returns pointer to it -- read, display, free in different functions
当我在 "main" 函数中动态分配内存时,程序运行良好。现在我想在 "read" 函数中分配,但每次尝试我都失败了。
我认为我的问题出在我的 "main" 函数中:我不知道如何从函数 "read" 中检索结构(指针)然后释放它是按函数动态分配的内存"destroy"。
int main(void)
{
int err_code;
struct student_t** s=(struct student_t**)malloc(1024*sizeof(struct student_t*));
**s = &read(err_code); //here is: error: lvalue required as unary '&' operand.
//But I think that my problem is wider than just this error.
if (s==NULL) {
puts("Error\n");
}
display(s);
destroy(s);
return err_code;
}
我试图做的是:创建一个结构类型的指针,指向结构的指针,由 "read" 函数返回。然后将此 ** 指针传递给 "destroy" 函数,以释放 malloc 的内存。
函数。
在函数 "read" 中,用户插入分配给结构的数据。 Returns 指向动态分配结构的指针,如果有任何错误,则为 NULL。
struct student_t* read(int *err_code)
{ printf("Insert data:\n");
struct student_t* p = (struct student_t *)malloc(1024*sizeof(struct student_t));
*err_code=1;
if (p==NULL) {
puts("Error\n");
return NULL;
}
//then it's supposed to read from user and assing to struct. Code below in links.
}
struct student_t {
char name[20];
char surname[40];
int index;
};
函数释放动态分配的内存,除非 "read" 失败并返回 NULL。
void destroy(struct student_t **s)
{
if (s!=NULL) free(s);
}
我的展示功能。但我想我的问题开始得更早。
void display(const struct student_t **s) //here I'm unsure if it should be *s- or **s-function.
{
if(s!=NULL) printf("%s %s, %i\n", (*s)->name, (*s)->surname, (*s)->index);
}
我的 "read" 函数基于我之前问题的答案。当我在 "main" 中正确分配内存时它会起作用。 "read" 我使用的代码:
其他更简单的 "read" 我无法正确处理我想要的所有错误:
非常感谢大家的帮助,一切都像是对我150个小时的救赎
努力完成一项任务。
您有两个个错误:
你问的那个是因为你做的都错了。函数返回的值是所谓的 r-value。之所以如此命名,是因为它只能位于赋值的右侧。它比那更复杂一点,但是 r 值或 l 值 的常见测试(您可以分配给 left-hand side) 是如果它的地址可以用地址运算符 &
获取。 R 值可以不 有一个地址。
对此的(简单)解决方案很简单:
*s = read(err_code);
第二个错误是因为 read
期望一个指向 int
的指针作为其参数,而您传递的是普通的 int
变量。这里你应该使用寻址运算符:
*s = read(&err_code);
还有一些其他问题,最大的问题是 s
需要成为一个指向指针的指针。不就是单指点吗,然后干脆做
struct student_t *s = read(&err_code);
另一个问题是,在许多系统中可能已经存在一个 read
函数(最值得注意的是 POSIX 系统,如 Linux 和 macOS),因此您将有冲突的声明那个函数。
当我在 "main" 函数中动态分配内存时,程序运行良好。现在我想在 "read" 函数中分配,但每次尝试我都失败了。
我认为我的问题出在我的 "main" 函数中:我不知道如何从函数 "read" 中检索结构(指针)然后释放它是按函数动态分配的内存"destroy"。
int main(void)
{
int err_code;
struct student_t** s=(struct student_t**)malloc(1024*sizeof(struct student_t*));
**s = &read(err_code); //here is: error: lvalue required as unary '&' operand.
//But I think that my problem is wider than just this error.
if (s==NULL) {
puts("Error\n");
}
display(s);
destroy(s);
return err_code;
}
我试图做的是:创建一个结构类型的指针,指向结构的指针,由 "read" 函数返回。然后将此 ** 指针传递给 "destroy" 函数,以释放 malloc 的内存。
函数。
在函数 "read" 中,用户插入分配给结构的数据。 Returns 指向动态分配结构的指针,如果有任何错误,则为 NULL。
struct student_t* read(int *err_code)
{ printf("Insert data:\n");
struct student_t* p = (struct student_t *)malloc(1024*sizeof(struct student_t));
*err_code=1;
if (p==NULL) {
puts("Error\n");
return NULL;
}
//then it's supposed to read from user and assing to struct. Code below in links.
}
struct student_t {
char name[20];
char surname[40];
int index;
};
函数释放动态分配的内存,除非 "read" 失败并返回 NULL。
void destroy(struct student_t **s)
{
if (s!=NULL) free(s);
}
我的展示功能。但我想我的问题开始得更早。
void display(const struct student_t **s) //here I'm unsure if it should be *s- or **s-function.
{
if(s!=NULL) printf("%s %s, %i\n", (*s)->name, (*s)->surname, (*s)->index);
}
我的 "read" 函数基于我之前问题的答案。当我在 "main" 中正确分配内存时它会起作用。 "read" 我使用的代码:
非常感谢大家的帮助,一切都像是对我150个小时的救赎 努力完成一项任务。
您有两个个错误:
你问的那个是因为你做的都错了。函数返回的值是所谓的 r-value。之所以如此命名,是因为它只能位于赋值的右侧。它比那更复杂一点,但是 r 值或 l 值 的常见测试(您可以分配给 left-hand side) 是如果它的地址可以用地址运算符
&
获取。 R 值可以不 有一个地址。对此的(简单)解决方案很简单:
*s = read(err_code);
第二个错误是因为
read
期望一个指向int
的指针作为其参数,而您传递的是普通的int
变量。这里你应该使用寻址运算符:*s = read(&err_code);
还有一些其他问题,最大的问题是 s
需要成为一个指向指针的指针。不就是单指点吗,然后干脆做
struct student_t *s = read(&err_code);
另一个问题是,在许多系统中可能已经存在一个 read
函数(最值得注意的是 POSIX 系统,如 Linux 和 macOS),因此您将有冲突的声明那个函数。