A pointer stores the address of an object in memory. If int number = 10;, then an int * pointer can store the address of number and access or change that value indirectly.
NULL before using it.int number = 10;
int *ptr = &number;
&number gives the address of number.ptr stores that address.*ptr dereferences the pointer and accesses the value stored at that address.#include <stdio.h>
int main(void) {
int number = 105;
int *ptr = &number;
printf("number = %d\n", number);
printf("*ptr = %d\n", *ptr);
printf("address of number = %p\n", (void *)&number);
printf("ptr = %p\n", (void *)ptr);
return 0;
}
The exact address changes between systems and program runs. Use %p with a void * value when printing an address.
#include <stdio.h>
int main(void) {
int number = 5;
int *ptr = &number;
*ptr = 100;
printf("number = %d\n", number);
return 0;
}
Output
number = 100
int number = 3;
int *int_ptr = &number;
double price = 12.5;
double *double_ptr = &price;
A pointer to int should point to an int object; a pointer to double should point to a double object. Incompatible pointer assignments normally require a diagnostic and should not be used to bypass the type system.
#include <stdio.h>
int main(void) {
int first = 100;
int second = 200;
int *p1 = &first;
int *p2 = &second;
int sum = *p1 + *p2;
printf("sum = %d\n", sum);
return 0;
}
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main(void) {
int first = 5;
int second = 10;
swap(&first, &second);
printf("first = %d, second = %d\n", first, second);
return 0;
}
Output
first = 10, second = 5
Passing addresses lets the function modify the caller's variables. This is a common C technique for functions that need to update multiple values.
#include <stdio.h>
double increased_salary(const double *basic) {
return *basic * 1.15;
}
int main(void) {
double salary = 1000.0;
printf("Final salary = %.2f\n", increased_salary(&salary));
return 0;
}
A const pointer parameter is useful when a function only needs to read the pointed-to value.
#include <stdio.h>
int main(void) {
int values[] = {3, 4, 5, 6, 7};
size_t count = sizeof values / sizeof values[0];
int *ptr = values;
for (size_t i = 0; i < count; i++) {
printf("value = %d, address = %p\n",
*ptr, (void *)ptr);
ptr++;
}
return 0;
}
In most expressions, an array name such as values is converted to a pointer to its first element. Pointer arithmetic such as ptr++ advances by one array element, not necessarily by one byte.
#include <stdio.h>
int sum_array(const int *values, size_t count) {
int sum = 0;
for (size_t i = 0; i < count; i++) {
sum += values[i];
}
return sum;
}
int main(void) {
int values[] = {1, 2, 3, 4, 5};
size_t count = sizeof values / sizeof values[0];
printf("Sum = %d\n", sum_array(values, count));
return 0;
}
#include <stdio.h>
int main(void) {
const char text[] = "plus2net";
const char *ptr = text;
while (*ptr != '\0') {
putchar(*ptr);
ptr++;
}
putchar('\n');
return 0;
}
int *ptr = NULL;
if (ptr != NULL) {
printf("%d\n", *ptr);
}
Never dereference a null pointer. The check must happen before using *ptr.
Practice with addresses, arrays, searching, swapping, sorting and strings, then compare your work with the complete solution page.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.