Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      const type qualifier

      From cppreference.com
      <c‎ |language
       
       
       
       

      Each individual type in the Ctype system has severalqualified versions of that type, corresponding to one, two, or all three of theconst,volatile, and, for pointers to object types,restrict qualifiers. This page describes the effects of theconst qualifier.

      Objectsdeclared with const-qualified types may be placed in read-only memory by the compiler, and if the address of a const object is never taken in a program, it may not be stored at all.

      Any attempt to modify an object whose type is const-qualified results in undefined behavior.

      constint n=1;// object of const-qualified typeint* p=(int*)&n;*p=2;// undefined behavior

      const semantics apply tolvalue expressions only; whenever a const lvalue expression is used in context that does not require an lvalue, itsconst qualifier is lost (note that volatile qualifier, if present, isn't lost).

      The lvalue expressions that designate objects of const-qualified type and the lvalue expressions that designate objects of struct or union type with at least one member of const-qualified type (including members of recursively contained aggregates or unions), are notmodifiable lvalues. In particular, they are not assignable:

      constint n=1;// object of const typen=2;// error: the type of n is const-qualified int x=2;// object of unqualified typeconstint* p=&x;*p=3;// error: the type of the lvalue *p is const-qualified struct{int a;constint b;} s1={.b=1}, s2={.b=2};s1= s2;// error: the type of s1 is unqualified, but it has a const member

      A member of a const-qualified structure or union type acquires the qualification of the type it belongs to (both when accessed using the. operator or the-> operator).

      struct s{int i;constint ci;} s;// the type of s.i is int, the type of s.ci is const intconststruct s cs;// the types of cs.i and cs.ci are both const int

      If an array type is declared with the const type qualifier (through the use oftypedef), the array type is not const-qualified, but its element type is.

      (until C23)

      An array type and its element type are always considered to be identically const-qualified.

      (since C23)
      typedefint A[2][3];const A a={{4,5,6},{7,8,9}};// array of array of const intint* pi= a[0];// Error: a[0] has type const int*void*unqual_ptr= a;// OK until C23; error since C23// Notes: clang applies the rule in C++/C23 even in C89-C17 modes

      If a function type is declared with the const type qualifier (through the use oftypedef), the behavior is undefined.

      In a function declaration, the keywordconst may appear inside the square brackets that are used to declare an array type of a function parameter. It qualifies the pointer type to which the array type is transformed.

      The following two declarations declare the same function:

      void f(double x[const],constdouble y[const]);void f(double*const x,constdouble*const y);
      (since C99)

      const-qualified compound literals do not necessarily designate distinct objects; they may share storage with other compound literals and with string literals that happen to have the same or overlapping representation.

      constint* p1=(constint[]){1,2,3};constint* p2=(constint[]){2,3,4};// the value of p2 may equal p1+1_Bool b="foobar"+3==(constchar[]){"bar"};// the value of b may be 1
      (since C99)

      A pointer to a non-const type can be implicitly converted to a pointer to const-qualified version of the same orcompatible type. The reverse conversion requires a cast expression.

      int* p=0;constint* cp= p;// OK: adds qualifiers (int to const int)p= cp;// Error: discards qualifiers (const int to int)p=(int*)cp;// OK: cast

      Note that pointer to pointer toT is not convertible to pointer to pointer toconst T; for two types to be compatible, their qualifications must be identical.

      char*p=0;constchar**cpp=&p;// Error: char* and const char* are not compatible typeschar*const*pcp=&p;// OK, adds qualifiers (char* to char*const)

      Contents

      [edit]Keywords

      const

      [edit]Notes

      C adopted theconst qualifier from C++, but unlike in C++, expressions of const-qualified type in C are notconstant expressions; they may not be used ascase labels or to initializestatic andthread storage duration objects,enumerators, orbit-field sizes. When they are used asarray sizes, the resulting arrays are VLAs.

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.7.3 Type qualifiers (p: 87-90)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.7.3 Type qualifiers (p: 121-123)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.7.3 Type qualifiers (p: 108-110)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 6.5.3 Type qualifiers

      [edit]See also

      C++ documentation forcv (const andvolatile) type qualifiers
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/const&oldid=154456"

      [8]ページ先頭

      ©2009-2025 Movatter.jp