[英]Pass pointer to struct by reference in C
Take in mind the following piece of code: 请记住以下代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int a;
int b;
int c;
}A;
A *test;
void init(A* a)
{
a->a = 3;
a->b = 2;
a->c = 1;
}
int main()
{
test = malloc(sizeof(A));
init(test);
printf("%d\n", test->a);
return 0;
}
It runs fine! 它运行正常! Now imagine that I want to use the
malloc
function outside the main
itself without returning a pointer to the struct
. 现在假设我想在
main
之外使用malloc
函数而不返回指向struct
的指针。 I would put malloc inside init
and pass test
adress. 我会将malloc放在
init
并通过test
地址。 But this doesnt seem to work. 但这似乎不起作用。
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int a;
int b;
int c;
}A;
A *test;
void init(A** a)
{
*a = malloc(sizeof(A));
*a->a = 3;
*a->b = 2;
*a->c = 1;
}
int main()
{
init(&test);
printf("%d\n", test->a);
return 0;
}
It keeps telling me that int a
(or b
/ c
) is not a member of the struct A
when I use the pointer. 它使告诉我,诠释
a
(或b
/ c
)不是的成员struct A
当我使用指针。
You must add parenthesis: 您必须添加括号:
void init(A **a)
{
*a = malloc(sizeof(A)); // bad you don't verify the return of malloc
(*a)->a = 3;
(*a)->b = 2;
(*a)->c = 1;
}
But it's good practice to do this: 但这样做是很好的做法:
void init(A **a)
{
A *ret = malloc(sizeof *ret); // we want the size that is referenced by ret
if (ret != NULL) { // you should check the return of malloc
ret->a = 3;
ret->b = 2;
ret->c = 1;
}
*a = ret;
}
You need to write (*a)->a = 3;
你需要写
(*a)->a = 3;
for reasons of precedence. 出于优先原因。
Your problem is operator precedence. 您的问题是运营商优先级。 The
->
operator has higher precedence than the *
(dereference) operator, so *a->a
is read as if it is *(a->a)
. ->
运算符的优先级高于*
(取消引用)运算符,因此*a->a
的读取就像它是*(a->a)
。 Change *a->a
to (*a)->a
: 将
*a->a
更改为(*a)->a
:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int a;
int b;
int c;
}A;
A *test;
void init(A** a)
{
*a = malloc(sizeof(A));
(*a)->a = 3;
(*a)->b = 2;
(*a)->c = 1;
}
int main()
{
init(&test);
printf("%d\n", test->a);
return 0;
}
Even though it's not a direct answer to your question, since we're in the vicinity of initialization I'd like to point out that C11 gives you a nicer syntax to initialize structs: 即使它不是你问题的直接答案,因为我们正处于初始化的附近,我想指出C11为你提供了一个更好的语法来初始化结构:
void init(A **a)
{
A *ret = malloc(sizeof *ret); // we want the size that is referenced by ret
if (ret != NULL) { // you should check the return of malloc
*ret = (A) {3, 2, 1};
// or
*ret = (A) { .a = 3, .b = 2, .c = 1 };
}
*a = ret;
}
Another advantage is that any uninitialized members are zeroed. 另一个优点是任何未初始化的成员都归零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.