简体   繁体   English

如何在C中通过引用将结构的成员传递给函数?

[英]How to pass a member of a struct to a function by reference in C?

I am trying to pass a member of a stuct to a function in c by reference and then use it to access the same member of a struct of the same type. 我试图通过引用将结构的成员传递给c中的函数,然后使用它来访问相同类型的结构的相同成员。

typedef struct Node
{
int a;
int b;
int c;
} Node;

Node mynode; //global

void pass_member(int *member){
   mynode.&member = 1;//gives a compiler error
}

void main()
{
 mynode.a = 0;
 mynode.b = 45;
 mynode.c = 64;

 pass_member(&mynode.a)
}

I can see that mynode.&member is not allowed in c but i think it shows what i am trying to do 我可以看到mynode.&member在c中是不允许的,但是我认为它表明了我正在尝试做的事情

is it possible to pass the address of an member of a struct to a function in c 是否可以将结构的成员地址传递给c中的函数
and then use it to access that member in a struct of the same type? 然后使用它来访问相同类型的结构中的成员?

basically what i am trying to achieve is a function somthing like this 基本上我想要实现的是这样的功能
void set_member(int value_to_set, int * pointer_to_member_of_which_to_set);

that would set a specific member of a global struct depending on the given member to the function. 这将根据函数的给定成员设置全局结构的特定成员。

I will give you some example first:- 我先给你一个例子:

You can do it like this func(&mynode) . 您可以这样执行func(&mynode)

And func is void func(Node* aa) funcvoid func(Node* aa)

Now you can use it like this 现在您可以像这样使用它

aa->a = aa->a + 1 etc. aa->a = aa->a + 1

Most of the time you will send the address of the whole structure rather than the address of the individiual element. 大多数情况下,您将发送整个结构的地址,而不是单个元素的地址。

  • Node aa => aa is of type struct node (typedefed to Node) Node aa => aa是struct node类型(类型定义为Node)

  • It's address: &aa 地址: &aa

  • aa.a is the element of the structure aa and it's address is &aa.a aa.a是结构aa的元素,其地址为&aa.a

Solution-1 解决方案 - 1

 void pass_member(int *member){ *member = 1; } 
  pass_member(&mynode.a) // in main()

Solution-2 解决方案-2

Also you can use it like this:- 您也可以像这样使用它:

typedef struct Node
{
int a;
int b;
int c;
} Node;

Node mynode; //global

void pass_member(Node *member){
   (*member).a = 1;//member->a = 1
}

void main()
{
 mynode.a = 0;
 mynode.b = 45;
 mynode.c = 64;

 pass_member(&mynode)
}

You asked is it possible to pass the address of an member of a struct to a function in c? 您问是否可以将结构成员的地址传递给c中的函数?

Ans: Yes it is possible, it is shown in the answer. 回答:是的,有可能,答案中会显示。

and then use it to access that member in a struct of the same type? 然后使用它来访问相同类型的结构中的成员?

You can access that memebr be it of some struct or some other struct. 您可以访问该memebr,无论是某些结构还是其他结构。 As long as it is a pointer to an appropriate type it's possible. 只要它是指向适当类型的指针,就可以。

You have two options, either pass pointer to the member variable as 您有两个选择,可以将指针传递给成员变量:

void pass_member(int *member){
  *member = 1;

and

 pass_member(&mynode.a);

OR pass pointer to the entire structure 或传递指向整个结构的指针

void pass_member(Node *member){
  (*member).a = 1; //or member->a = 1; as pointed out in comments

and

pass_member(&mynode)

You can use offsetof to get the relative address of a member. 您可以使用offsetof获得成员的相对地址。 I am unaware of a reverse operation (don't do much in C), but it can be simulated like this: 我不知道反向操作(在C语言中不做很多事情),但是可以这样模拟:

#define offsetin(s,a,t) *((t*)(((char*)&s)+a))

Then your code would look like this: 然后您的代码将如下所示:

void pass_member(size_t member){
    offsetin(mynode, member, int) = 1;
}

void main()
{
    mynode.a = 0;
    mynode.b = 45;
    mynode.c = 64;

    pass_member(offsetof(Node,a));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM