Pointer Basics in C Programming (Complete Guide with Examples)


This tutorial explains pointers in C, variables that store the memory address of another variable. It covers declaration, initialization, accessing values via pointers, and practical examples, helping beginners understand memory management and address manipulation.

1. What is a Pointer

  1. A pointer is a variable that stores the address of another variable.
  2. Using pointers, you can directly access and modify memory locations.
  3. Syntax to declare a pointer:

data_type *pointer_name;

2. Pointer Declaration and Initialization


int *ptr; // pointer to an integer
float *fptr; // pointer to a float
char *cptr; // pointer to a char

int x = 10;
ptr = &x; // & operator gives the address of x

3. Accessing Values Using Pointers

  1. * operator (dereference operator) is used to access the value stored at the address:

int x = 10;
int *ptr = &x;

printf("Address of x: %p\n", ptr);
printf("Value of x using pointer: %d\n", *ptr);

Output:


Address of x: 0x7ffee3b8a8c
Value of x using pointer: 10

4. Example Program: Basic Pointer Usage


#include <stdio.h>

int main() {
int num = 25;
int *p; // pointer declaration

p = &num; // assign address of num to pointer

printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value using pointer: %d\n", *p);
printf("Pointer itself (address stored): %p\n", p);

return 0;
}

Sample Output:


Value of num: 25
Address of num: 0x7ffee3b8a8c
Value using pointer: 25
Pointer itself (address stored): 0x7ffee3b8a8c

5. Key Points to Remember

  1. Pointers store addresses, not actual values
  2. & operator gives the address of a variable
  3. * operator accesses the value stored at a memory address
  4. Pointers can be used with all data types (int, float, char, etc.)
  5. Essential for dynamic memory allocation, arrays, and function calls by reference