Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Pass By Reference C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Arrays of Structs C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example C Memory Mistakes

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C Problem Solving

C Problem Solving C Build the Logic C Edge Cases & Errors C Test Cases

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers C Command-Line Args

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Hash Tables C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C Pointers


Share

Creating Pointers

You learned from the previous chapter, that we can get the memory address of a variable with the reference operator &:

Example

int myAge = 43; // an int variable

printf("%d", myAge);  // Outputs the value of myAge (43)
printf("%p", &myAge); // Outputs the memory address of myAge (0x7ffe5367e044)
Try it Yourself »

A pointer is a variable that stores the memory address of another variable as its value.

A pointer variable points to a variable of a specific data type (like int), and is created with the * operator.

The address of the variable you are working with is assigned to the pointer:

Example

int myAge = 43;     // An int variable
int* ptr = &myAge;  // A pointer variable, with the name ptr, that stores the address of myAge

// Output the value of myAge (43)
printf("%d\n", myAge);

// Output the memory address of myAge (0x7ffe5367e044)
printf("%p\n", &myAge);

// Output the memory address of myAge with the pointer (0x7ffe5367e044)
printf("%p\n", ptr);
Try it Yourself »

Example explained

Create a pointer variable with the name ptr, that points to an int variable (myAge). Note that the type of the pointer has to match the type of the variable you're working with (int in our example).

Use the & operator to get the memory address of the myAge variable, and assign it to the pointer.

Now, ptr holds the value of myAge's memory address.


Dereference

In the example above, we used the pointer variable to get the memory address of a variable (used together with the & reference operator).

You can also get the value of the variable the pointer points to, by using the * operator (the dereference operator):

Example

int myAge = 43;     // Variable declaration
int* ptr = &myAge;  // Pointer declaration

// Reference: Output the memory address of myAge with the pointer (0x7ffe5367e044)
printf("%p\n", ptr);

// Dereference: Output the value of myAge with the pointer (43)
printf("%d\n", *ptr);
Try it Yourself »

You can also use the dereference operator to change the value of the variable that the pointer points to:

Example

int myAge = 43;     // Variable declaration
int* ptr = &myAge;  // Pointer declaration

// Change the value of myAge with the pointer
*ptr = 50;

// Output the new value of myAge (50)
printf("%d\n", myAge);
Try it Yourself »

Note that the * sign can be confusing here, as it does two different things in our code:

  • When used in declaration (int* ptr), it creates a pointer variable.
  • When not used in declaration, it acts as a dereference operator.

Visualize Pointers

Choose an expression below to see which part of the memory it reads:

Code


Memory

Output

Explanation



Two Ways to Declare Pointers

There are two ways to declare pointer variables in C:

int* myNum;
int *myNum;

Both ways do exactly the same thing, so it is up to you which one you use. Just use the same style in all your code.

Note: If you declare more than one variable on the same line, the * only belongs to the first one. int* a, b; creates a pointer named a, and a normal int named b. To create two pointers, write int *a, *b;


Notes on Pointers

Pointers are one of the things that make C stand out from other programming languages, like Python and Java.

They are important in C, because they let you work directly with the data in the computer's memory. For example, a function can change the value of a variable if it gets a pointer to it. This is how scanf() stores the user's input in your variable. You will learn how to do this yourself in the Pass By Reference chapter.

Pointers are also used to build data structures like lists, trees and graphs. And sometimes you even have to use pointers, for example when working with files and memory management.

But be careful; pointers must be handled with care, since it is possible to damage data stored in other memory addresses.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.