简体   繁体   English

双链表2

[英]Double linked list2

I did a lot of linked lists already, but I didn't use them for a while and didn't really programm anything, therefore I am litterally lost.我已经做了很多链表,但是我有一段时间没有使用它们,也没有真正编程任何东西,因此我完全迷失了。 The following is my Code:以下是我的代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int age; 
    int pers_num;
    char name[100];
    void *previous;
    void *next;
    
}Person;

int main(){
    Person first_element;
    Person last_element;
    first_element.next = (Person *)(&last_element);
    last_element.age = 19;
    printf("%d\n",(&last_element)->age);
    printf("%d\n",(first_element.next)->age);
    
    }

Could you explain to me why following error is thrown?你能解释一下为什么会抛出以下错误吗?

speicher.c: In function 'main':
speicher.c:22:39: warning: dereferencing 'void *' pointer
   23 |     printf("%d\n",(first_element.next)->age);
      |                                       ^~
speicher.c:23:39: error: request for member 'age' in something not a structure or union

As I understand it "first_element.next" should be the pointer which points at last_element.据我了解,“first_element.next”应该是指向last_element的指针。 therefore you should be able to use the -> to access the data inside last_element.因此,您应该能够使用 -> 访问 last_element 中的数据。 The line 22 Runs perfectly even thought it should have the same Output as line 23, where the error is thrown.第 22 行运行完美,甚至认为它应该具有与第 23 行相同的 Output,其中引发了错误。

You can't dereference a void pointer because it's a pointer that references a void type:-) In other words, it doesn't really know what actual type it points at.您不能取消引用 void 指针,因为它是一个引用 void 类型的指针:-) 换句话说,它并不真正知道它指向的实际类型。 The reason why the two lines behave differently are, keeping in mind that a->b is just syntactic sugar for (*a).b :这两行表现不同的原因是,请记住a->b只是(*a).b语法糖:

printf("%d\n",(&last)->age);
    // This (&last)->age, which is actually the same
    // as last.age. In both cases, the type being used
    // to get the age field is Person, so no problem.

printf("%d\n",(first.next)->age);
    // This (first.next)->age is the same as
    // (*(first.next)).age. Because first.next is of type
    // void *, it cannot be dereferenced.

What you are doing is akin to declaring a variable with void x , which is not valid (don't confuse that with void *x which is valid).您所做的类似于用void x声明一个变量,这是无效的(不要将其与有效的void *x混淆)。

You would be better off (within the structure) pointing to the actual type you want, with something like:您最好(在结构内)指向您想要的实际类型,例如:

typedef struct s_Person { // A
    int age; 
    int pers_num;
    char name[100];
    struct s_Person *previous, *next;
} Person; // B

Note that you cannot use the Person type definition while you're creating it, since it does not yet exist.请注意,在创建Person类型定义时不能使用它,因为它还不存在。 Simplistically, you can think of it coming into existence at the B point.简单地说,你可以认为它在B点出现。 The named structure s_Person comes into existence at A so it can be used within the structure.命名结构s_Person存在于A中,因此可以在结构中使用。

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

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