[英]Pointer to table of struct in C
I don't understand some piece of code in C , and couldn't find any similar question, so I hope you'll help me. 我不了解C中的某些代码,也找不到任何类似的问题,所以希望您能对我有所帮助。 I have a
struct
table defined like this: 我有一个这样定义的
struct
表:
struct my_struct {
struct other_struct some_struct[N];
char some_char;
..
} my_struct[N];
In another function: 在另一个功能中:
struct my_struct *ms;
And then is the part I don't understand: 然后是我不了解的部分:
ms = &my_struct[0];
How can I interpret this line? 我如何解释这一行?
ms
is a pointer to the struct my_struct
. ms
是指向struct my_struct
的指针。 It contains address of a struct my_struct
variable. 它包含
struct my_struct
变量的地址。 Here we assign to ms
the already declared struct my_struct
array's (also name my_struct
) 0-th element's address. 在这里,我们将已经声明的
struct my_struct
数组(也称为my_struct
)的第0个元素的地址分配给ms
。
&
- address of operator. &
-运营商地址。 Which basically returns the address of a variable. 它基本上返回变量的地址。
Now you can access my_struct[0]
via ms
. 现在,您可以通过
ms
访问my_struct[0]
。
Equivalently 等价
ms->some_char = 'A'
is same as my_struct[0].some_char='A'
. ms->some_char = 'A'
与my_struct[0].some_char='A'
。 To give a small example I can simplify this way. 举一个小例子,我可以简化这种方式。
struct a{
int z;
};
struct a array[10]; // array of 10 `struct a`
struct a* ptr = &array[0]; // ptr contains the address of array[0].
Now we can access array[0]
via pointer ptr
. 现在我们可以通过指针
ptr
访问array[0]
。
And ms
is just a pointer to struct
not pointer to a table of struct
as you have mentioned in the question heading. ms
只是pointer to struct
的pointer to struct
而不是您在问题标题中提到的pointer to struct
pointer to a table of struct
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.