← Back to C Course | Chapter 8: Structures & Unions | Lesson 4 of 7

C Structure & Pointers

A structure pointer holds the address of a structure, like a note saying where a form is filed. The arrow operator reaches into it.
Syntax
c
struct StructName *pointer_name = &variable_name;

pointer_name->member;    // arrow operator
(*pointer_name).member;  // equivalent

What is a Structure Pointer?

This is the natural counterpart to how a structure variable holds the data directly -- a structure pointer instead holds where that data lives, which is essential once you start allocating structures dynamically with malloc().

Example: What is a Structure Pointer?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
	struct Point p = {5};
	struct Point *ptr = &p;
	// Print formatted text to the screen
	printf("%d", ptr->x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

The Arrow Operator (->)

ptr->name is exactly equivalent to (*ptr).name, but is far more readable -- in practice almost all real-world C code uses the arrow operator rather than the dot-and-parentheses form.

Example: The Arrow Operator (->)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Student
struct Student { char name[20]; };
// Program execution starts in main()
int main() {
	struct Student s = {"Alice"};
	struct Student *ptr = &s;
	// Print formatted text to the screen
	printf("%s", ptr->name);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dereferencing with Dot Notation

The parentheses around *ptr are mandatory because the dot operator (.) binds more tightly than the dereference operator (*) -- writing *ptr.member without parentheses would incorrectly try to dereference ptr.member instead.

Example: Dereferencing with Dot Notation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
	struct Point p = {5};
	struct Point *ptr = &p;
	// Print formatted text to the screen
	printf("%d", (*ptr).x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dynamically Allocating Structures

This is exactly how you build linked lists, trees, and other dynamic data structures in C, since each new node's memory doesn't exist until you explicitly request it at runtime with malloc(sizeof(struct Node)).

Example: Dynamically Allocating Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	struct Point *p = malloc(sizeof(struct Point));
	p->x = 10;
	// Print formatted text to the screen
	printf("%d", p->x);
	// Release the heap memory so it can be reused
	free(p);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointers inside Structures

A struct Node containing a Node *next member is the fundamental building block of linked lists -- each node stores the address of the next one, letting you chain together an arbitrary number of elements without a fixed array size.

Example: Pointers inside Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Node
struct Node {
	int value;
	struct Node *next;
};
// Program execution starts in main()
int main() {
	struct Node second = {2, NULL};
	struct Node first = {1, &second};
	// Print formatted text to the screen
	printf("%d %d", first.value, first.next->value);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using . instead of -> on a pointer to a struct.
  2. Using a struct pointer before allocating memory.
  3. Forgetting to free a dynamically allocated struct.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.