限定符const可以应用于任何变量的声明,以指定其值不会被更改(这取决于存储const变量的位置,我们可以通过使用指针来更改const变量的值)。如果尝试更改const,则结果是实现定义的。
1)指向变量的指针。

int *ptr;

我们可以改变ptr的值,我们也可以改变指向的对象ptr的值。指针指向的指针和值都存储在读写区域中。请参阅以下代码片段。

#include <stdio.h>
int main(void)
{
    int i = 10;
    int j = 20;
    int *ptr = &i;        /* pointer to integer */
    printf("*ptr: %d\n", *ptr);
 
    /* pointer is pointing to another variable */
    ptr = &j;
    printf("*ptr: %d\n", *ptr);
 
    /* we can change value stored by pointer */
    *ptr = 100;
    printf("*ptr: %d\n", *ptr);
 
    return 0;
}

输出:

    * ptr:10
    * ptr:20
    * ptr:100


2)指向常量的指针。

指向常量的指针可以通过以下两种方式声明。

const int *ptr;

要么

int const *ptr;

我们可以将指针更改为指向任何其他整数变量,但不能使用指针ptr更改指向的对象(实体)的值。指针存储在读写区域(在本例中为堆栈)。指向的对象可以是只读或读写区域。让我们看看以下示例。

#include <stdio.h>
int main(void)
{
    int i = 10;  
    int j = 20;
    const int *ptr = &i;    /* ptr is pointer to constant */
 
    printf("ptr: %d\n", *ptr);
    *ptr = 100;        /* error: object pointed cannot be modified
                     using the pointer ptr */
 
    ptr = &j;          /* valid */
    printf("ptr: %d\n", *ptr);
 
    return 0;
}

输出:

 错误:分配只读位置'* ptr'

以下是变量i本身不变的另一个例子。

#include <stdio.h>
int main(void)
    int const i = 10;    /* i is stored in read only area*/
    int j = 20;
    int const *ptr = &i;        /* pointer to integer constant. Here i
                                 is of type "const int", and &i is of
                                 type "const int *".  And p is of type                           
                                "const int", types are matching no issue */
    printf("ptr: %d\n", *ptr);
    *ptr = 100;        /* error */
    ptr = &j;          /* valid. We call it as up qualification. In
                         C/C++, the type of "int *" is allowed to up
                         qualify to the type "const int *". The type of
                         &j is "int *" and is implicitly up qualified by
                         the compiler to "cons tint *" */
    printf("ptr: %d\n", *ptr);
    return 0;
}

输出:

 错误:分配只读位置'* ptr'

C ++中不允许使用Down资格,并且可能会在C中引发警告。以下是另一个资格认证的示例。

#include <stdio.h>
int main(void)
{
    int i = 10;
    int const j = 20;
    /* ptr is pointing an integer object */
    int *ptr = &i;
    printf("*ptr: %d\n", *ptr);
    /* The below assignment is invalid in C++, results in error
       In C, the compiler *may* throw a warning, but casting is
       implicitly allowed */
    ptr = &j;
    /* In C++, it is called 'down qualification'. The type of expression
       &j is "const int *" and the type of ptr is "int *". The
       assignment "ptr = &j" causes to implicitly remove const-ness
       from the expression &j. C++ being more type restrictive, will not
       allow implicit down qualification. However, C++ allows implicit
       up qualification. The reason being, const qualified identifiers
       are bound to be placed in read-only memory (but not always). If
       C++ allows above kind of assignment (ptr = &j), we can use 'ptr'
       to modify value of j which is in read-only memory. The
       consequences are implementation dependent, the program may fail
       at runtime. So strict type checking helps clean code. */
    printf("*ptr: %d\n", *ptr);
    return 0;
}
// More interesting stuff on C/C++ @ 


3)指向变量的常量指针。

int *const ptr;

上面的声明是指向整型变量的常量指针,意味着我们可以改变指针指向的对象的值,但不能改变指向另一个变量的指针。

#include <stdio.h>
 
int main(void)
{
   int i = 10;
   int j = 20;
   int *const ptr = &i;    /* constant pointer to integer */
 
   printf("ptr: %d\n", *ptr);
 
   *ptr = 100;    /* valid */
   printf("ptr: %d\n", *ptr);
 
   ptr = &j;        /* error */
   return 0;
}

输出:

 错误:分配只读变量'ptr'

4)常量指针

const int *const ptr;

上面的声明是指向常量变量的常量指针,这意味着我们不能改变指针指向的值,也不能将指针指向其他变量。让我们看一下例子。

#include <stdio.h>
 
int main(void)
{
    int i = 10;
    int j = 20;
    const int *const ptr = &i;        /* constant pointer to constant integer */
 
    printf("ptr: %d\n", *ptr);
 
    ptr = &j;            /* error */
    *ptr = 100;        /* error */
 
    return 0;
}

输出:

     错误:分配只读变量'ptr'
     错误:分配只读位置'* ptr'

C中的Const限定符-IDC帮帮忙