← Back to C Course | Chapter 7: Pointers | Lesson 2 of 9

C Pointers Introduction

A pointer is a variable that holds the address of another variable, like a note saying which locker something is in. You follow the note to reach the thing.
Syntax
c
data_type *pointer_name = &variable_name;

*pointer_name;  // the value the pointer points to

What is a Pointer?

This indirection is the foundation of dynamic memory, arrays, and passing large data efficiently between functions -- instead of copying a whole struct or array, you can pass just its address, which is always a fixed, small size.

Example: What is a Pointer?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	int *ptr = &x;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Declaring Pointers

int *ptr declares ptr as 'pointer to int', meaning it can only validly hold the address of an int variable; assigning it the address of a float or char variable will compile with a warning but produce incorrect behavior later.

Example: Declaring Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare count and set it to 5
	int count = 5;
	int *ptr = &count;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

The Address-Of Operator (&)

Writing &count gives you the address where count actually lives in memory, which you then typically assign to a pointer variable, e.g. int *p = &count -- this is the operation you use to connect a pointer to a real variable.

Example: The Address-Of Operator (&)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare count and set it to 5
	int count = 5;
	int *p = &count;
	// Print formatted text to the screen
	printf("%p", (void*)p);
	// Return 0 to signal that the program finished successfully
	return 0;
}

The Dereferencing Operator ()

*ptr = 10 doesn't change ptr itself; it changes the value stored at the address ptr is currently pointing to -- this distinction between changing a pointer and changing what it points to is one of the trickiest parts of learning pointers.

Example: The Dereferencing Operator ()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	int *ptr = &x;
	*ptr = 10;
	// Print formatted text to the screen
	printf("%d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Null Pointers

Dereferencing a NULL pointer (writing *ptr when ptr is NULL) is one of the most common causes of a program crash (segmentation fault), so checking if (ptr != NULL) before dereferencing is a standard defensive habit.

Example: Null Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Check whether ptr != NULL
	if (ptr != NULL) {
		// Print formatted text to the screen
		printf("%d", *ptr);
	} else {
		// Print formatted text to the screen
		printf("Pointer is null");
	}
	// 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 a pointer before it points anywhere, such as int *p; *p = 5;, which writes to a random address and can crash.
  2. Writing int* a, b; and assuming both are pointers, when only a is a pointer and b is an int.
  3. Confusing &x (address of x) with *p (the value p points to) and printing an address when a value was intended.

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.