Skip to content

Instantly share code, notes, and snippets.

@abserari
Created November 16, 2020 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abserari/6be611af1ec366e747f809c4024088b2 to your computer and use it in GitHub Desktop.
Save abserari/6be611af1ec366e747f809c4024088b2 to your computer and use it in GitHub Desktop.
how list head could find the nesting structure
#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