Home > Backend Development > C++ > Why Are `const T *obj` and `T const *obj` Both Valid in C , and When Should I Use Each?

Why Are `const T *obj` and `T const *obj` Both Valid in C , and When Should I Use Each?

Patricia Arquette
Release: 2024-12-11 10:24:12
Original
941 people have browsed it

Why Are `const T *obj` and `T const *obj` Both Valid in C  , and When Should I Use Each?

Const Data: Two Declared Paths

In C , you can make an object's data or pointer unmodifiable using const. The traditional syntax for this is:

const T *obj; // Pointer to const data
T const *obj; // Equivalent to const T*
Copy after login

However, you may have noticed an alternative syntax:

Object const *obj;
Copy after login

This raises the question: why are both syntaxes valid, and when should you use one over the other?

The Birth of Const Data

The positioning of const within type declarations stems from the early days of C. The language grammar defined by Kernighan and Ritchie allowed for const data to be declared as:

const T *obj;
Copy after login

In essence, the C compiler parsed tokens from left to right, applying const to the appropriate type specification based on the token's position.

The Const Qualifier's Application

The position of const within type declarations does not affect the result because const always applies to the declaration to its left. In the case of pointers:

  • const T *obj: Const applies to the data pointed to by obj.
  • T const *obj: Const still applies to the data pointed to by obj.

Semantic Equivalence

The underlying reason for this equivalence is that the semantic meaning of the declaration remains the same regardless of const's position. Whether you use const T *obj or T const *obj, you protect the data pointed to by obj from modification.

Function Pointers: A Similar Case

A similar situation arises when declaring function pointers:

void * function1(void); // Function returning void *
void (* function2)(void); // Function pointer to a function returning void
Copy after login

Again, the left-to-right parsing of the language syntax allows for these alternative declarations.

Preference and Use Cases

Ultimately, the preference for one syntax over another is subjective. However, it's generally recommended to use:

  • const T *obj: If you specifically need to emphasize that the data pointed to is const.
  • T const *obj: For easier reading in scenarios where const applies to the data type itself.

Regardless of your preference, both syntaxes are valid and semantics remain consistent.

The above is the detailed content of Why Are `const T *obj` and `T const *obj` Both Valid in C , and When Should I Use Each?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template