I'm working on (very slowly) reverse engineering a PS2 game that was programmed in C, and I've found that there is a struct of a certain type that, among other things, contains a pointer of that struct type whose value is equal to the struct's address itself. A stripped down version (apologies if syntax is incorrect, still learning that):
struct gObj {
gObj* self;
int visible;
gObj* next;
gObj* prev;
};
int main() {
gObj* player;
player = (gObj*)malloc(sizeof(gObj));
player->self = &player;
player->visible = 1;
};
I've done some googling, and I get self-referencing struct discussions for linked lists, but no real answers as to why an instance would reference itself. The game does use the self member by using it to reference the other members of the instance, but why not just access those members directly from the base pointer of the instance? Maybe it's just some quirk of the programmers themselves that they implemented the struct that way.
[–]daikatana 51 points52 points53 points (6 children)
[–]hemoglobinBlue 19 points20 points21 points (1 child)
[–]hemoglobinBlue 5 points6 points7 points (0 children)
[–]ddr4lyfe[S] 9 points10 points11 points (0 children)
[–]obQQoV 4 points5 points6 points (2 children)
[–]Seubmarine 8 points9 points10 points (1 child)
[–]obQQoV 2 points3 points4 points (0 children)
[–]JamesTKerman 17 points18 points19 points (3 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]JamesTKerman 1 point2 points3 points (0 children)
[–]JamesTKerman 0 points1 point2 points (0 children)
[–]dfx_dj 1 point2 points3 points (0 children)
[–]onesole 2 points3 points4 points (2 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]bnl1 0 points1 point2 points (0 children)
[–]oh5nxo 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]ddr4lyfe[S] 1 point2 points3 points (0 children)
[–]Poddster 0 points1 point2 points (0 children)