C语言自引用指针(Self Referential Pointer)解析及相关问题咨询
Hey there! Let’s tackle all your questions about self-referential pointers in C clearly and thoroughly.
First, let’s clarify the difference between self-referential data types and self-referential pointers:
- A self-referential data type (like a linked list node) is a structure that contains a pointer to another instance of the same structure type.
- A self-referential pointer, on the other hand, is a pointer variable that points to its own memory address. In other words, the value stored in the pointer is exactly the address of the pointer variable itself.
Here’s a straightforward example demonstrating a self-referential pointer, plus a note on how it differs from self-referential structures:
Self-referential pointer example
#include <stdio.h> int main() { // Create a self-referential pointer int *p = (int*) &p; // Print to verify: both should be the same address printf("Address of pointer p: %p\n", &p); printf("Value stored in p (which points to itself): %p\n", p); // WARNING: Dereferencing p (*p) is undefined behavior! // Because p is an int* pointing to an int* variable, not an int. // printf("%d", *p); // Don't do this! return 0; }
Self-referential structure (for context)
This is a common use case people confuse with self-referential pointers, but it’s a related concept:
// A self-referential data type (linked list node) typedef struct Node { int data; struct Node *next; // Pointer to another Node instance, not itself } Node;
This statement comes from a type-safety perspective in C:
- In C, pointers have strict type rules: a pointer of type
T*should point to an object of typeT. - A
void*is a generic pointer type that can legally point to any type of object—including another pointer (like itself). For example,void* p = &p;is valid without any casting, becausevoid*accepts any pointer address. - For specific pointer types (like
int*),int* p = &p;is a type mismatch:&pis of typeint**(pointer to int pointer), which can’t be assigned toint*directly. You need a cast to make it compile, which breaks strict type safety.
So the quote means void* is the only pointer type that can be self-referential without relying on unsafe type casting.
int *p = (int*) &p; Let’s unpack this line step by step:
&p: This takes the memory address of the pointer variablepitself. The type of this expression isint**(a pointer to an int pointer).(int*): This is a type cast, converting theint**value to anint*type. This bypasses C’s type checking to make the assignment valid.int *p = ...: We assign the casted address top, so nowpstores its own memory address—making it a self-referential pointer.
Important note: While this compiles, it’s not type-safe. Dereferencing p (using *p) will lead to undefined behavior, because you’re treating the address of a pointer as an integer value, which is not meaningful in most cases.
Here are some great resources to deepen your understanding of pointers and C in general:
- The C Programming Language (K&R): The definitive guide to C. It doesn’t focus specifically on self-referential pointers, but its explanations of pointer fundamentals and type systems will help you grasp the concept intuitively.
- C Primer Plus: A beginner-friendly book with detailed examples of pointer usage, including self-referential structures (like linked lists) that build the foundation for understanding self-referential pointers.
- ISO C Standards (C99/C11/C17): For deep dives into the formal rules of pointer types and conversions. You can find draft versions online for free.
- University C Course Materials: Many computer science programs have public lecture notes or tutorials that cover pointer nuances, including self-referential patterns in data structures like trees and linked lists.
内容的提问来源于stack exchange,提问作者rooni

