C Pointers Introduction
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 (&)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 ()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Using a pointer before it points anywhere, such as
int *p; *p = 5;, which writes to a random address and can crash. - Writing
int* a, b;and assuming both are pointers, when onlyais a pointer andbis anint. - Confusing
&x(address of x) with*p(the value p points to) and printing an address when a value was intended.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: