79325369

Date: 2025-01-03 02:35:03
Score: 0.5
Natty:
Report link

I am not able to understand how placing the next pointer on top will help here.

This is an ancient C equivalent of C++ inherited class. Typecasting is needed in order to use common linked list functions with different node types. The first member of a struct is guaranteed to have offset 0 within a struct. Example old code using classic Microsoft naming conventions.

#include <stdio.h>

typedef struct BASENODE_{
  struct BASENODE_* next;
}BASENODE;

typedef struct INTNODE_{
  struct INTNODE_* next;
  int data;
}INTNODE;

BASENODE* NextNode(BASENODE* pNode)
{
    if(pNode == NULL)
        return pNode;
    return pNode->next;
}

int main(int argc, char**argv)
{
INTNODE aIntNode[8];
INTNODE* pNode;
int i;
    for(i = 0; i < 8; i++){
        aIntNode[i].next = &aIntNode[i+1];
        aIntNode[i].data = i;
    }
    aIntNode[7].next = NULL;
    pNode = aIntNode+0;
    while(pNode != NULL){
        printf("%d\n", pNode->data);
        pNode = (INTNODE *) NextNode((BASENODE*) pNode);
    }
    return(0);
}
Reasons:
  • Blacklisted phrase (1): I am not able to
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: tstdmy