C Nodes and Links

Intermediate
6 min read
Updated: Nov 2026
1 example

Nodes and Their Links

A node is a struct containing data and a pointer to its own type: struct Node *next. A struct cannot contain itself, but it can contain a pointer to itself. Allocate nodes with malloc, initialize both fields, and link by assigning next.

Remember
struct Node *n = malloc(sizeof *n);
if (!n) { /* out of memory */ }
n->data = 5;
n->next = NULL;
a->next = n;        /* link a to n */

Cheat Sheet

Keep these facts handy while you work with Nodes and Links in C.

TopicDetail
Self referencePointer to the same struct type
Allocatemalloc(sizeof *n)
Linkprev->next = node
FreeEach node once

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Creating a Linked List.

Example 1 — Allocate nodes and follow links

Read through a chain of next pointers, then free the nodes.

C
#include <stdio.h>
#include <stdlib.h>

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

static struct Node *new_node(int v) {
    struct Node *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->data = v;
    n->next = NULL;
    return n;
}

int main(void) {
    struct Node *a = new_node(1);
    a->next = new_node(2);
    a->next->next = new_node(3);

    printf("third value: %d\n", a->next->next->data);
    printf("last next is NULL: %d\n", a->next->next->next == NULL);

    free(a->next->next);
    free(a->next);
    free(a);
    return 0;
}

How It Works

  1. new_node allocates and initializes both fields.
  2. Chaining -> follows links one at a time.
  3. Each node is freed once, last-to-first here because the chain is short.

Edge Cases & Pitfalls

Check these before relying on Nodes and Links in real programs.

uninit

Uninitialized next

A new node whose next is not set holds garbage. Always set it to NULL or a node.

order

Freeing the link too early

free(a); a->next reads freed memory. Save the pointer first.

Key Takeaways

  • Self pointer: allowed.
  • Initialize both fields.
  • Free once per node.

One line: A node is a struct containing data and a pointer to its own type: struct Node *next.

Frequently Asked Questions

A pointer has a fixed size, so the compiler knows how large the struct is. A struct containing itself would have infinite size.
It marks the end of the list: there is no following node.
Continue with "C Creating a Linked List" next. Build a list from data by appending nodes to the tail. The C Linked Lists hub lists every topic in order.

Next: Creating a Linked List

Build a list from data by appending nodes to the tail.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful