简体   繁体   English

指向作为结构成员的结构的指针

[英]Pointers to structures as structure members

I am working my way through Sam's Teach Yourself C in 21 Days and I've run into a code snippet that I can't wrap my head around.我在 21 天内完成了 Sam 的自学 C 的工作,我遇到了一个我无法理解的代码片段。 This is from Day 11, Exercise 6. The task is "write the definition for a structure type named data that can hold a string of up to 20 characters."这是第 11 天的练习 6。任务是“为名为data的结构类型编写定义,该结构类型可以容纳最多 20 个字符的字符串。” My solution was我的解决方案是

struct data{
    char a[21];
};

Dumb use of a structure, but ok.愚蠢地使用结构,但没关系。 My dummy code compiles and produces the expected result of 21我的虚拟代码编译并产生 21 的预期结果

#include <stdio.h>

struct data{
    char a[21];
} data_inst;

int main(void)
{
    printf("%lu\n", sizeof(data_inst.a));

    return 0;
}

However, the answer according to the book is但是,根据书上的答案是

struct data{
    char a[21];
    struct data *ptr;
};

My interpretation of the correct answer is that ptr is a pointer to type data .我对正确答案的解释是ptr是指向data类型的指针。 But ptr is also a member of data .ptr也是data的成员。 What is the point of this additional line?这条附加线有什么意义? What is the benefit of a structure that contains a pointer to itself?包含指向自身的指针的结构有什么好处? Or am I just missing something completely here?或者我只是在这里完全错过了一些东西?

A pointer in the structure is used to store the address of the next structure it's generally used in the linked list where we want to store the address of the next node so it becomes connected.结构中的指针用于存储下一个结构的地址,它通常用于链表中,我们要在其中存储下一个节点的地址,以便它连接起来。 You can search about the linked list you will get detailed information that how the linked list works and according to your code, there is no need for struct data *ptr because you only need to make a structure with 20 characters.您可以搜索链表,您将获得有关链表如何工作的详细信息,根据您的代码,不需要struct data *ptr因为您只需要制作一个 20 个字符的结构。 and for your question what is the use of struct data *ptr you can refer to the linked list.对于您的问题struct data *ptr的用途是什么,您可以参考链表。

The pointer does not point to this structure, but to a structure of this type.指针并不指向这个结构,而是指向这个类型的结构。 Sure it could point to itself, but it can also point to another instance of the same struct type.当然它可以指向自己,但它也可以指向相同结构类型的另一个实例。

So you can keep or point to a first instance of data , and if necessary, it can in turn point to another one and so on.因此,您可以保留或指向data的第一个实例,如果有必要,它可以反过来指向另一个实例,依此类推。 This would allow you to keep up to 20 characters (or sacrificing null string termination, up to 21 with unambiguously) in one such instance, but use more than one instance to either: 1/ store strings exceeding 21 characters over more than one instance 2/ store a list or stack of strings of up to 21 characters这将允许您在一个这样的实例中保留最多 20 个字符(或牺牲 null 字符串终止,最多 21 个明确),但使用多个实例来: 1/ 在多个实例上存储超过 21 个字符的字符串 2 / 存储最多 21 个字符的列表或字符串堆栈

If you define string as proper c style null terminated string, then of course it can only be up to 20. Otherwise you can use null for strings of length less than 21, and just avoid using null for strings of length 21 - you know that there can be nothing after 21st character anyway. If you define string as proper c style null terminated string, then of course it can only be up to 20. Otherwise you can use null for strings of length less than 21, and just avoid using null for strings of length 21 - you know that无论如何,在第 21 个字符之后就什么都没有了。

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

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