简体   繁体   English

struct args中的函数指针?

[英]Function pointer in struct args?

foo is a struct with 5 scalar variables ( A , B , C , D , F ) and one array ( E ). foo是一个struct与5个标量变量( ABCDF )和一个阵列( E )。 What is confusing me is what f[0] , f[1] , and f[2] are in this context and what is happening here. 令我困惑的是f[0]f[1]f[2]在这种情况下是什么以及这里发生了什么。

int     
bar(struct foo *f)
{
    f[1].C = f[0].B > f[2].C;
    f[0].E[-1] = f[0].D;
    f[0].A = f[1].C;
}

Are f[0] , f[1] , and f[2] individual structures with member variables? f[0]f[1]f[2]具有成员变量的个别结构? Can someone please explain? 有人可以解释一下吗? Thanks. 谢谢。

Are f[0] , f[1] , and f[2] individual structures with member variables? f[0]f[1]f[2]具有成员变量的个别结构?

Yes. 是。 f is a pointer to an array of struct foo instances f[0] is the first such member of that array, f[1] is the second member, etc. You might call it like this: f是指向struct foo实例数组的指针f[0]是该数组的第一个这样的成员, f[1]是第二个成员,等等。你可以像这样调用它:

struct foo fArray[3];
// ... Initialize fArray[0], fArray[1], fArray[2] etc. ...
bar(fArray);

What you are doing is, you are passing a reference (pointer) to an array of struct foo to the function bar . 你正在做的是,你将一个struct foo数组的引用(指针)传递给函数bar

You must somewhere have a code that is similar to following: 你必须在某个地方有一个类似于以下的代码:

struct foo  myFoos[10]; // an array with 10 elements of struct foo
struct foo *mallocedFoos;
// here goes some code to initialize the elements of the array
bar(&myFoos[0]);          // pass a reference to (address of/pointer to) the array

// or something like this is happening
mallocedFoos = malloc(sizeof(struct foo) * 10);
// here goes some code to initialize allocated memory
bar(mallocedFoos);        // pass the 'struct foo *' to the function

To understand the concept better, see this example. 要更好地理解这个概念,请参阅示例。

Yes, in this context, f[0] , f[1] , etc., are elements of an array of type struct foo . 是的,在这种情况下, f[0]f[1]等是struct foo类型数组的元素。

The more interesting thing to me is this line: 对我来说更有趣的是这一行:

f[0].E[-1] = f[0].D;

I didn't realize negative indexes were allowed, but this question explains that array indexing is just pointer math, so it's an obfuscated way of saying: 我没有意识到允许负面索引,但是这个问题解释了数组索引只是指针数学,所以这是一种模糊的说法:

f[0].D = f[0].D;

Which is basically useless as far as I know. 据我所知,这基本上没用。

Also interesting: 也很有趣:

f[0].C = f[0].B > f[2].C;

This would set f[0].C to a boolean, which is not usually compared with a > operator, so it's possible that different C members are used for different functions. 这会将f[0].C设置为布尔值,通常不会与>运算符进行比较,因此不同的C成员可能会用于不同的函数。

I feel that your confusion is warranted, given the strange nature of this function. 考虑到这个功能的奇特性,我觉得你的困惑是有道理的。

In this case f is an array of structures Similar to 在这种情况下, f是一个类似于array of structures
struct node = { int A; //A to D and F are scalar variables int B; int C; int D; int E[10]; //E is an array of integers int F; } struct node = { int A; //A to D and F are scalar variables int B; int C; int D; int E[10]; //E is an array of integers int F; } struct node f[10]; //f is an array of structs struct node = { int A; //A to D and F are scalar variables int B; int C; int D; int E[10]; //E is an array of integers int F; } struct node f[10]; //f is an array of structs struct node f[10]; //f is an array of structs

For more details you can also refer How do you make an array of structs in C? 有关更多详细信息,您还可以参考如何在C中创建结构数组?

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

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