Created
November 16, 2020 04:47
-
-
Save abserari/6be611af1ec366e747f809c4024088b2 to your computer and use it in GitHub Desktop.
how list head could find the nesting structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stddef.h> | |
typedef struct List { | |
struct List *next; | |
struct List *prev; | |
} List; | |
typedef struct devices { | |
int first; | |
struct List list; | |
int second; | |
} devices; | |
int main() { | |
devices a; | |
devices b; | |
List listA; | |
List listB; | |
a.first = 1; | |
a.list = listA; | |
b.first = 2; | |
b.list = listB; | |
a.list.next = &b.list; | |
b.list.prev = &a.list; | |
int o = (int)(&((struct devices*)0)->list); | |
devices * actualBAddress = (devices*)((char *)a.list.next - o); | |
printf("%p\n", &b); | |
printf("%p\n", actualBAddress); | |
printf("%d\n", actualBAddress->first); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment