[*]У меня есть структура Node, первым членом которой является Node* p_next
[*]У меня есть указатель на первый узел называется p_head
[*]Я хочу преобразовать &p_head, который имеет тип Node**, в Node* и сохранить его в p_node_before_head. Это сделано для того, чтобы я мог рассматривать *p_node_before_head как если бы это был узел, где значение p_node_before_head->p_next является значением p_head
Код: Выделить всё
#include
struct Node{
Node* p_next;
int item;
};
int main(){
Node head = {nullptr, 5};
Node* p_head = &head;
//By pretending that "p_head" is a node whose first element is a pointer to the head,
//we create a new pointer that points to this "node before the head"
Node* p_node_before_head = reinterpret_cast(&p_head);
Node* p_head2 = p_node_before_head->p_next;
std::cout item
Подробнее здесь: [url]https://stackoverflow.com/questions/79131163/is-casting-the-address-of-a-pointer-to-a-struct-to-the-address-of-a-struct-wh[/url]