What are Pointers in C Programming? See Example

Pointers are one of the most powerful and important features of the C programming language. They allow direct access to memory locations, making C extremely flexible and efficient.


1. Understanding Pointers

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

Key Characteristics

  • Points to a specific memory location
  • Can store addresses of variables, arrays, functions, and more
  • Enables dynamic memory allocation
  • Used for efficient parameter passing

Basic Concept

If x is a variable stored at memory address 1000, a pointer can store that address.

int x = 10;
int *ptr = &x;   // ptr stores the address of x

2. Accessing the Address of a Variable

In C, the address of a variable is obtained using the address-of operator (&).

Example

int x = 25;
printf("%p", &x);   // prints memory address of x

This prints a value like:
0x7ffee12bc8ac


3. Declaration and Initialization of Pointer Variables

Syntax

data_type *pointer_name;

Example

int *p;        // pointer to int
float *q;      // pointer to float
char *c;       // pointer to char

Initialization

int x = 10;
int *p = &x;

Here:

  • p stores the address of x
  • *p gives the value stored at that address

4. Accessing a Variable Through Its Pointer (Dereferencing)

Dereferencing means accessing the value at the memory address stored in the pointer.
Use the asterisk operator (*).

Example

int x = 100;
int *ptr = &x;

printf("%d", *ptr);   // Output: 100

If you modify the value using a pointer:

*ptr = 200;
printf("%d", x);     // Output: 200

Pointers directly modify the original variable.


5. Pointers and Arrays

Arrays and pointers are closely related.
In C, the name of an array represents the address of its first element.

Example

int arr[5] = {10, 20, 30, 40, 50};

printf("%p", arr);       // address of arr[0]
printf("%p", &arr[0]);   // same result

So:

int *p = arr;

Now:

  • p points to arr[0]
  • p+1 points to arr[1]

5.1 Accessing Array Elements Using Pointers

int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;

for(int i = 0; i < 5; i++) {
    printf("%d ", *(p+i));
}

Explanation

  • *(p + i) dereferences the ith element
  • Equivalent to arr[i]

5.2 Pointer Arithmetic

Pointers support:

  • p++ → move to next element
  • p-- → move to previous element
  • p+i → jump forward
  • p-i → jump backward

Example:

p = arr;     // points to arr[0]
p++;         // now points to arr[1]

5.3 Passing Arrays to Functions Using Pointers

When you pass an array, you are actually passing a pointer.

Example

void display(int *p, int n) {
    for(int i = 0; i < n; i++) {
        printf("%d ", p[i]);
    }
}

int main() {
    int arr[] = {10, 20, 30};
    display(arr, 3);
}

6. Why Pointers Are Important

Pointers enable:

  • Efficient memory manipulation
  • Dynamic memory allocation (malloc, calloc)
  • Fast array and string operations
  • Passing large structures without copying
  • Implementing linked lists, trees, graphs

C is widely known as a pointer-friendly language, essential for system programming.


Conclusion

Pointers provide low-level memory access and allow efficient handling of arrays, structures, strings, and dynamic memory. Understanding pointers is crucial for writing optimized and powerful C programs.


📚 Citations

🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming/

🔗 External C Documentation:
https://www.w3schools.com/c/

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *