DEV Community

Aditya Pratap Bhuyan
Aditya Pratap Bhuyan

Posted on

Are there any best practices for pointer declarations in C/C++ that improve code readability and maintainability?

Yes, there are several conventions and best practices for pointer declarations in C/C++ that can significantly improve code readability, maintainability, and reduce bugs. While the compiler accepts multiple styles, consistent use of a well-understood convention is critical for working in teams or maintaining large codebases. Here's a breakdown:


✅ 1. Consistent Placement of Asterisk (*) in Declarations

One of the most debated style conventions is where to place the asterisk (*) in pointer declarations.

Common styles:

  • int* ptr; — emphasizes that ptr is a pointer to an int.
  • int *ptr; — emphasizes the relationship between the variable and the pointer symbol.
  • int * ptr; — less common; may look cluttered.

Best practice:
Use int* ptr; to indicate clearly that ptr is a pointer to an int. But be aware: in declarations like int* ptr1, ptr2;, only ptr1 is a pointer — ptr2 is just an int. This can confuse readers, so it's better to declare pointers one per line:

int* ptr1;
int ptr2;
Enter fullscreen mode Exit fullscreen mode

✅ 2. Declare One Pointer Per Line

Avoid declaring multiple variables in the same line when mixing pointers and non-pointers. For example:

int* a, b;  // b is NOT a pointer!
Enter fullscreen mode Exit fullscreen mode

Instead, write:

int* a;
int b;
Enter fullscreen mode Exit fullscreen mode

This avoids ambiguity and makes the pointer status of each variable explicit.


✅ 3. Use nullptr in C++ (Not NULL or 0)

In C++, use nullptr instead of NULL or 0 when initializing pointers. nullptr is type-safe and unambiguous.

int* ptr = nullptr;  // Correct
Enter fullscreen mode Exit fullscreen mode

In C, NULL is still standard, but avoid using 0 for clarity.


✅ 4. Avoid Pointer Arithmetic Unless Necessary

Pointer arithmetic can be error-prone and hard to read. Prefer using array indexing unless performance is critical or the logic is clearer with pointers.

Instead of:

*(ptr + i)
Enter fullscreen mode Exit fullscreen mode

Prefer:

ptr[i]
Enter fullscreen mode Exit fullscreen mode

✅ 5. Always Initialize Pointers

Uninitialized pointers can lead to undefined behavior. Always initialize pointers at the time of declaration if possible:

int* ptr = nullptr;  // Safer than leaving it uninitialized
Enter fullscreen mode Exit fullscreen mode

✅ 6. Use const Correctly with Pointers

Mark what is constant — the pointer or the pointee — to communicate intent clearly.

  • const int* ptr; → pointer to constant int (you can't modify the value).
  • int* const ptr; → constant pointer to int (you can't change the address).
  • const int* const ptr; → constant pointer to constant int.

Use const as much as possible to prevent unintended modifications.


✅ 7. Use Smart Pointers in Modern C++

In modern C++ (C++11 and later), prefer smart pointers (std::unique_ptr, std::shared_ptr) over raw pointers for resource management:

std::unique_ptr<MyClass> ptr = std::make_unique<MyClass>();
Enter fullscreen mode Exit fullscreen mode

Smart pointers automatically handle memory deallocation, reducing memory leaks and improving safety.


✅ 8. Comment Pointer Intent

Sometimes it helps to clarify why a pointer is used:

// Pointer to dynamically allocated array of integers
int* numbers = new int[10];
Enter fullscreen mode Exit fullscreen mode

Especially in function arguments, indicate whether the pointer is expected to be modified, dereferenced, or used as an output parameter.


✅ 9. Use Meaningful Pointer Names

Avoid vague names like ptr, p, or temp. Instead, use descriptive names that convey the purpose:

char* buffer;
int* nextNode;
float* sensorReadings;
Enter fullscreen mode Exit fullscreen mode

This improves readability and reduces cognitive load.


✅ 10. Avoid Pointer Aliasing When Possible

Aliasing (multiple pointers to the same object) can lead to hard-to-track bugs. If unavoidable, document it clearly or consider using references in C++ instead.


✅ Summary of Pointer Best Practices

  • Prefer int* ptr; syntax, but always declare one pointer per line.
  • Use nullptr in C++ for safety and clarity.
  • Always initialize pointers and use const wisely.
  • Favor smart pointers in modern C++ for ownership semantics.
  • Avoid pointer arithmetic when possible and prefer array notation.
  • Use meaningful names and comments to clarify intent.

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas

The placement of * for pointers should be "east" (to the right) — See "West Pointers" here.

nullptr is now also standard in C23.

While it's generally a good idea to initialize variables upon declaration, it's not universally so. It can hide other bugs. Given:

int x_min = 0, x_max = 0;
int y_min = 0, y_max = 0;
// ...
x_min = MIN( x1, x2 );
y_min = MIN( y1, y2 );
x_max = MAX( x1, x2 );
x_max = MAX( y1, y2 );
draw_rect( x_min, y_min, x_max, y_max );
Enter fullscreen mode Exit fullscreen mode

That code will compile without warning despite the bug. (Did you notice it?) It goes unnoticed because the compiler has no way to know there's a typo. Because y_max was initialized on line 2, the code is “fine” as far as the compiler is concerned. Had you left all of x_min, x_max, y_min, and y_max uninitialized in their declarations, the compiler could have warned you about the bug.

References are just as prone to aliasing as pointers are.