Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Pointers in C: A Comprehensive Guide to Types, Usage, and Best Practices

What is a Pointer in C?

A pointer is a variable that stores the memory address of another variable. By referencing the address, a pointer can indirectly access or modify the data it points to. This indirection enables efficient memory usage and fast execution, especially when dealing with large data structures or dynamic memory allocation.

How to Use Pointers in C

Consider an integer variable v:

int v = 0;
Its value is 0, but every variable also has a unique address. You can retrieve this address with the address-of operator (&):
printf("Address of v: %p\n", (void*)&v);
The printed address will look like a random hexadecimal number and may differ on each run.

Below is a simple program that demonstrates how a pointer stores an address:

#include <stdio.h>
int main() {
    int v = 0;
    int *y = &v;        // y holds the address of v
    printf("Address stored in y: %p\n", (void*)y);
    printf("Value pointed to by y: %d\n", *y);
    return 0;
}

Typical output:

Address stored in y: 0x7ffeefbff5c8
Value pointed to by y: 0

Declaring a Pointer

Pointer declarations follow the pattern:

data_type *pointer_name;
Where data_type is the type of data the pointer references. Valid examples include:

int    *ptr_int;    // pointer to int
double *ptr_double;  // pointer to double
char   *ptr_char;    // pointer to char

Initializing a Pointer

After declaration, a pointer must be initialized with a valid address. Using an uninitialized pointer leads to undefined behavior. Initialization syntax:

pointer = &variable;
Example:
#include <stdio.h>
int main() {
    int a = 10;
    int *p = &a;
    printf("Address in p: %p\n", (void*)p);
    printf("Value via p: %d\n", *p);
    return 0;
}

Types of Pointers in C

Direct vs. Indirect Access

Direct access uses the variable name directly; indirect access uses a pointer. Example:

#include <stdio.h>
int var = 1;
int *ptr = &var;

printf("Direct: %d\n", var);      // 1
printf("Indirect: %d\n", *ptr);   // 1

*ptr = 48; // modify via pointer
printf("After change, indirect: %d\n", *ptr); // 48

Pointer Arithmetic

When a pointer points to an array element, adding or subtracting an integer moves the pointer by that many elements, not bytes. Example:

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

for (int i = 0; i < 5; ++i) {
    printf("%d\n", *p);
    ++p; // move to next element
}

Key rules:

Pointers with Arrays

Pointers provide a concise way to iterate over array elements:

int a[5] = {1,2,3,4,5};
int *p = a; // same as &a[0]
for (int i = 0; i < 5; ++i) {
    printf("%d\n", *p++); // prints 1 through 5
}

Pointers and Strings

A string is an array of char terminated by a null character. Pointer manipulation makes string traversal trivial:

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello Guru99!";
    char *p = str;
    printf("First character: %c\n", *p);
    ++p;
    printf("Next character: %c\n", *p);

    // Print entire string
    for (int i = 0; i < strlen(str); ++i) {
        printf("%c\n", *p++);
    }
    return 0;
}

Advantages of Pointers

Potential Pitfalls

Key Takeaways

C Language

  1. Master C# Variables & Primitive Data Types: A Complete Guide
  2. Four Proven Patterns for User‑Defined Functions in C
  3. C++ Programming Basics: What Is C++ and Why It Matters
  4. Mastering C++ Pointers: Concepts, Examples & Practical Applications
  5. C Programming Language: Fundamentals, History, and Applications
  6. Function Pointers in C: Practical Examples and Best Practices
  7. Understanding Java Variable Types: A Comprehensive Guide
  8. Mastering C Pointers: Practical Steps to Advanced Programming
  9. C++ Variable Types Explained: Memory, Limits, and Operations
  10. Python Variable Types: Understanding and Using Data Types