This might be a way to detect memory corruption. If memory is written out of bounds, you can always tell if player is valid because you know its address, and if its self member is anything but its address, something bad happened. The actual code that does this may not be present in the release build, but the pointer remained. Edit: Or, conversely, you can tell if a pointer to an object is valid if that object contains as its first member a pointer to itself. Answer from daikatana on reddit.com
🌐
Reddit
reddit.com › r/c_programming › a struct instance containing a pointer to itself?
r/C_Programming on Reddit: A struct instance containing a pointer to itself?
December 22, 2023 -

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.

🌐
C-faq
c-faq.com › decl › selfrefstruct.html
comp.lang.c FAQ list · Question 1.14
typedef struct { char *item; NODEPTR next; } *NODEPTR; but the compiler gave me error messages. Can't a structure in C contain a pointer to itself? A: Structures in C can certainly contain pointers to themselves; the discussion and example in section 6.5 of K&R make this clear. The problem ...
Top answer
1 of 3
8

You can use a forward declaration of the struct

typedef struct sNode Node; // this is a typedef and a declaration of the struct
struct sNode{
    int data;
    Node *next;
    Node *prev;
};

This way Node is known (but not defined), in the definition of your struct.

This can be compressed as it is done by Yunnosch. But then you need to use the struct Name notation inside your declaration.

This way it is possible to already use the typedefed name also the forward declaration is necessary if you have some circular dependencies in your structs.

It is also possible to use the struct name as the typedef:

typedef struct Node Node; 
struct Node{
    int data;
    Node *next;
    Node *prev;
};

I personally prefer the first style, it seems "clearer" to me, but there is nothing wrong with the second example, as long as the compiler is not from the pre-standard era (before 1989).

As Jens Gustedt pointed out the first style might be incompatible if this is included in C++.

So maybe I should change my preference to the first.

2 of 3
6

Inside the typedef, the to-be-defined type is not yet known, so you need to introduce and use a struct tag:

typedef struct Node_tag {
    int data;
    struct Node_tag *next;
    struct Node_tag *prev;
} Node;

The Node_tag is the struct tag, because of where it is introduced (and not because of the name part "_tag"). It is not a type in itself, only the combination as struct Node_tag is a type which can be used for the struct members.
Afterwards, when the typedef is done, the type Node has been defined.
To clarify, a second typedef would be possible as typedef struct Node_tag NodeToo;. This demonstrates that the type struct Node_tag is also useable. That is why I prefer to use the "_tag" name fragment, to allow to be clear of what is used.

🌐
Trending source
trendingsource.github.io › 2024-02-02-everything-you-need-to-know-about-self-referential-structures-in-c
Everything You Need to Know About Self-Referential Structures in C | Trending source
January 2, 2024 - Self-referential structures, also known as recursive data structures, are a powerful concept in computer science that allows data to reference itself in some way. In the C programming language, self-referential structures are declared using ...
🌐
Reddit
reddit.com › r/c_programming › [deleted by user]
typedef a struct with a pointer to itself : r/C_Programming
April 21, 2023 - If you really wanted to you could use a void pointer, which would let you keep the struct unnamed. However, if you do this: typedef struct foo_t { int a; struct foo_t* b; } foo_t; You can refer to the struct as foo_t OR struct foo_t in the rest of your code. I agree with the first commenter ...
🌐
Quora
quora.com › How-can-I-have-a-function-pointer-inside-a-struct-that-takes-itself-as-an-argument-in-C
How to have a function pointer inside a struct that takes itself as an argument in C - Quora
Answer (1 of 5): Give it up. Quit trying to “objectize” C. structs are passive objects, they do not need to carry around the utility functions that you use with them. C has a global namespace structure, so regular functions work just fine, exactly the same, and much easier to construct ...
🌐
Bytes
bytes.com › home › forum › topic
How to make a struct contain a ptr to itself? - C / C++
April 3, 2006 - Michael Mair <Michael.Mair@i nvalid.invalid> wrote: [color=blue] > Steve Edwards schrieb:[color=green] > > > > How do I create a new type of structure that contains a pointer to its > > own type? i.e. > > > > typedef struct{ > > string tag; > > TagPtr *subPtr; > > } TagPtr;[/color][/color] [color=blue] > Basically, you want > > struct TagPtr_ { > string tag; > struct TagPtr_ *subPtr; > }; > typedef struct TagPtr_ TagPtr;[/color] In this, and all other examples, you can leave off the underscore. struct TagPtr does not clash with typedef TagPtr. (The same is true for unions.) Richard ... Re: How to make a struct contain a ptr to itself? Richard Bos schrieb:[color=blue] > Michael Mair <Michael.Mair@i nvalid.invalid> wrote:[color=green] >>Steve Edwards schrieb: >>[color=darkred] >>>How do I create a new type of structure that contains a pointer to its >>>own type?
Find elsewhere
🌐
Quora
quora.com › Why-do-we-use-a-struct-pointer-inside-a-structure-definition-in-C
Why do we use a struct pointer inside a structure definition in C? - Quora
Below are the main motivations, mechanics, and examples. ... Forward referencing / recursive types: a struct cannot contain an instance of itself because that would require infinite size.
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › cs31 › resources › C-structs_pointers.php
CS31: Intro to C Structs and Pointers
See struct.c for more examples. Exercise: implement and test two functions in this file: printStudent and initStudent. C pointer variables A pointer variable stores the address of a memory location that stores a value of the type to which it points ("a level of indirection").
🌐
University of Toronto
cs.toronto.edu › ~heap › 270F02 › node31.html
But first, structs and malloc
#include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *n1, *n2, *n3; The (possibly) odd feature of the declaration of struct node is that it includes a pointer to itself. From the point-of-view of the compiler, it ensures that struct node has ...
🌐
Stack Overflow
stackoverflow.com › questions › 47981605 › c-struct-with-pointer-to-itself-accessing-values
C struct with pointer to itself - accessing values - Stack Overflow
December 27, 2017 - I have a problem with structs. I must use a struct that has a pointer to itself. I have done this and I think it works: typedef struct tag_t tag_t; struct tag_t{ char name[15]; int
🌐
GeeksforGeeks
geeksforgeeks.org › c language › structure-pointer-in-c
Structure Pointer in C - GeeksforGeeks
A structure pointer is a pointer variable that stores the address of a structure. It allows the programmer to manipulate the structure and its members directly by referencing their memory location rather than passing the structure itself.
Published   December 23, 2024
🌐
Quora
quora.com › What-mistake-happens-in-C-when-a-struct-is-assigned-to-itself
What mistake happens in C when a struct is assigned to itself? - Quora
Answer (1 of 12): My intuition is that this is a question that has been asked in an academic context as part of an exercise/homework etc. So a lot of that is to figure out what the person who is asking the question really means, especially as this is an extremely ambiguously worded question. I ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_self_referential_structures.htm
Self-referential Structures in C
Multi-dimensional Arrays ... A self-referential structure is a struct data type in C, where one or more of its elements are pointer to variables of its own type. Self-referential user-defined types are of immense use in C programming.
🌐
Stack Overflow
stackoverflow.com › questions › 33028308
c - How to make use of a structure pointer inside the structure itself - Stack Overflow
It is used to avoid the need to say "struct msg* link;" inside the struct definition. As answered in the comment above, the queue is represented by a pointer to the first item, which has a pointer to the second (if any), and so on until you reach a NULL pointer. It's important to take care when building such lists that no node points to itself or to any precursor, or you get an infinite loop chasing to the tail.
🌐
Reddit
reddit.com › r/cprogramming › struggling to understand structure pointers
r/cprogramming on Reddit: Struggling to understand Structure Pointers
December 29, 2023 -

I am a newbie to C. I recently learned about pointers and am comfortable with int pointers. However, I am having a hard time with structures.For instance, if I give a pointer to a structure how does the pointer point to all the data stored in the struct? How would that even be possible?Let me give an example -

int digit = 5
int *ptr = &digit; // Let's assume &digit is 1001 
// Therefore ptr is now 1001

However, a structure is a contigous block of memory. What would a pointer to it store?

Top answer
1 of 5
14
struct is a continuous block of memory, with the fields inside the struct with a fixed layout. Therefore, if you know the address of the struct, and you have the struct definition, you also know the addresses of all the fields in the struct. If you know the address of an integer (for example). You know the address of an integer. It doesn't matter if the integer is inside of a struct, in an array, nor does it matter if it is in stack, or allocated with malloc or a global variable. If you have a valid address of a valid integer, you have an address of an integer. Also, memory addresses are at byte accuracy. Not all members of a struct have the same address, in fact they all have a different address, and only the first member has same address as the whole struct (but different pointer type).
2 of 5
9
#include #include // Simple example struct struct s { int a; int b; }; int main() { // Instantiate a struct. struct s s1 = {1, 2}; // Get a pointer to the struct. struct s *s_ptr = &s1; // Print the value of the pointer, which is the address of s1. printf("address: %p\n", s_ptr); // Access s1's member `a` through the pointer with the arrow operator. printf("member a: %d\n", s_ptr->a); // Access s1's member `b` through the pointer with a dereference and a dot. // The parentheses are necessary because the dot operator has precedence // over the dereference. printf("member b: %d\n", (*s_ptr).b); return 0; } Example output: address: 0x7ffcd540fc30 member a: 1 member b: 2