C语言 常量指针 vs 指向常量的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21476869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:42:47  来源:igfitidea点击:

Constant pointer vs Pointer to constant

cpointersconst

提问by Venkatesh K

I want to know the difference between

我想知道两者的区别

const int* ptr;

and

int * const ptr; 

and how it works.

以及它是如何工作的。

It is pretty difficult for me to understand or keep remember this. Please help.

我很难理解或记住这一点。请帮忙。

回答by haccks

const int* ptr; 

declares ptra pointer to const inttype. You can modify ptritself but the object pointed to by ptrshall not be modified.

声明ptr一个指向const int类型的指针。可以修改ptr自身,但不能修改指向的对象ptr

const int a = 10;
const int* ptr = &a;  
*ptr = 5; // wrong
ptr++;    // right  

While

尽管

int * const ptr;  

declares ptra constpointer to inttype. You are not allowed to modify ptrbut the object pointed to by ptr.

声明ptr一个const指向int类型的指针。您不能修改.ptr指向的对象ptr

int a = 10;
int *const ptr = &a;  
*ptr = 5; // right
ptr++;    // wrong

Generally I would prefer the declaration like this which make it easy to read and understand (read from right to left):

一般来说,我更喜欢这样的声明,它易于阅读和理解(从右到左阅读):

int const  *ptr; // ptr is a pointer to constant int 
int *const ptr;  // ptr is a constant pointer to int

回答by Itachi

const int * ptr;

means that the pointed data is constant and immutable but the pointer is not.

意味着指向的数据是常量和不可变的,但指针不是。

int * const ptr;

means that the pointer is constant and immutable but the pointed data is not.

意味着指针是常量和不可变的,但指向的数据不是。

回答by MustafaP

1) Constant Pointers :These type of pointers are the one which cannot change address they are pointing to. This means that suppose there is a pointer which points to a variable (or stores the address of that variable). Now if we try to point the pointer to some other variable (or try to make the pointer store address of some other variable), then constant pointers are incapable of this.

1)常量指针:这些类型的指针是不能改变它们指向的地址的指针。这意味着假设有一个指向变量的指针(或存储该变量的地址)。现在,如果我们尝试将指针指向某个其他变量(或尝试使指针存储某个其他变量的地址),那么常量指针就无法做到这一点。

A constant pointer is declared as : int *const ptr( the location of 'const' make the pointer 'ptr' as constant pointer)

常量指针声明为:(int *const ptr“const”的位置使指针“ptr”成为常量指针)

2) Pointer to Constant :These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.

2)指向常量的指针这些类型的指针不能改变它们指向的值。这意味着他们不能改变他们所持有的地址的变量的值。

A pointer to a constant is declared as : const int *ptr(the location of 'const' makes the pointer 'ptr' as a pointer to constant.

指向常量的指针声明为:(const int *ptr“const”的位置使指针“ptr”成为指向常量的指针。

Example

例子

Constant Pointer

常量指针

#include<stdio.h>

int main(void)
{
    int a[] = {10,11};
    int* const ptr = a;

    *ptr = 11;

    printf("\n value at ptr is  : [%d]\n",*ptr);
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    ptr++;
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    return 0;
}

Now, when we compile the above code, compiler complains :

现在,当我们编译上面的代码时,编译器抱怨:

practice # gcc -Wall constant_pointer.c -o constant_pointer
constant_pointer.c: In function ‘main':
constant_pointer.c:13: error: increment of read-only variable ‘ptr'

Hence we see very clearly above that compiler complains that we cannot changes the address held by a constant pointer.

因此,我们在上面非常清楚地看到编译器抱怨我们无法更改常量指针持有的地址。

Pointer to Constants

指向常量的指针

#include<stdio.h>

int main(void)
{
    int a = 10;
    const int* ptr = &a;


    printf("\n value at ptr is  : [%d]\n",*ptr);
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    *ptr = 11;

    return 0;
}

Now, when the above code is compiled, the compiler complains :

现在,当编译上述代码时,编译器会抱怨:

practice # gcc -Wall pointer_to_constant.c -o pointer_to_constant
pointer_to_constant.c: In function ‘main':
pointer_to_constant.c:12: error: assignment of read-only location ‘*ptr'

Hence here too we see that compiler does not allow the pointer to a constant to change the value of the variable being pointed.

因此在这里我们也看到编译器不允许指向常量的指针改变被指向的变量的值。

Quotation

引述

回答by G one

Referncing from This Thread

参考自 This Thread

Constant Pointers

常量指针

Lets first understand what a constant pointer is. A constant pointer is a pointer that cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

让我们首先了解什么是常量指针。常量指针是不能改变其持有地址的指针。换句话说,我们可以说,一旦常量指针指向一个变量,它就不能指向任何其他变量。

A constant pointer is declared as follows :
<type of pointer> * const <name of pointer>
An example declaration would look like :
int * const ptr;
Lets take a small code to illustrate these type of pointers :

常量指针声明如下:
<type of pointer> * const <name of pointer>
示例声明如下所示:
int * const ptr;
让我们用一段小代码来说明这些类型的指针:

#include<stdio.h>

int main(void)
{
    int var1 = 0, var2 = 0;
    int *const ptr = &var1;
    ptr = &var2;
    printf("%d\n", *ptr);

    return 0;
} 

In the above example :

在上面的例子中:

  • We declared two variables var1 and var2
  • A constant pointer ‘ptr' was declared and made to point var1
  • Next, ptr is made to point var2.
  • Finally, we try to print the value ptr is pointing to.
  • 我们声明了两个变量 var1 和 var2
  • 声明了一个常量指针 'ptr' 并使其指向 var1
  • 接下来,使 ptr 指向 var2。
  • 最后,我们尝试打印 ptr 指向的值。


Pointer to Constant

指向常量的指针

As evident from the name, a pointer through which one cannot change the value of variable it points is known as a pointer to constant. These type of pointers can change the address they point to but cannot change the value kept at those address.

从名字可以看出,一个不能改变它所指向的变量值的指针被称为常量指针。这些类型的指针可以更改它们指向的地址,但不能更改保存在这些地址处的值。

A pointer to constant is defined as : const <type of pointer>* <name of pointer>An example of definition could be : const int* ptr;Lets take a small code to illustrate a pointer to a constant :

一个指向常量的指针被定义为: 定义的 const <type of pointer>* <name of pointer>一个例子可以是: const int* ptr;让我们用一段小代码来说明一个指向一个常量的指针:

 #include<stdio.h>

int main(void)
{
    int var1 = 0;
    const int* ptr = &var1;
    *ptr = 1;
    printf("%d\n", *ptr);

    return 0;
} 

In the code above :

在上面的代码中:

  • We defined a variable var1 with value 0
  • we defined a pointer to a constant which points to variable var1
  • Now, through this pointer we tried to change the value of var1
  • Used printf to print the new value.
  • 我们定义了一个值为 0 的变量 var1
  • 我们定义了一个指向常量的指针,该指针指向变量 var1
  • 现在,我们试图通过这个指针改变 var1 的值
  • 使用 printf 打印新值。

回答by Sergey L.

const int* ptr;

is a pointer to constant (content). You are allowed to modify the pointer. e.g. ptr = NULL, ptr++, but modification of the content is notpossible.

是一个指向常量(内容)的指针。您可以修改指针。例如ptr = NULLptr++,但无法修改内容。

int * const ptr;

Is a constant pointer. The opposite is possible. You are notallowed to modify the pointer, but you areallowed to modify what it points to e.g. *ptr += 5.

是一个常量指针。相反是可能的。你是不是允许修改指针,但你可以修改它指向如*ptr += 5

回答by user253751

int i;
int j;

int * const ptr1 = &i;

The compiler will stop you changing ptr1.

编译器将阻止您更改ptr1.

const int * ptr2 = &i;

The compiler will stop you changing *ptr2.

编译器将阻止您更改*ptr2.

ptr1 = &j; // error
*ptr1 = 7; // ok

ptr2 = &j; // ok
*ptr2 = 7; // error

Note that you can still change *ptr2, just not by literally typing *ptr2:

请注意,您仍然可以更改*ptr2,而不仅仅是直接键入*ptr2

i = 4;
printf("before: %d\n", *ptr2); // prints 4
i = 5;
printf("after: %d\n", *ptr2); // prints 5
*ptr2 = 6; // still an error

You can also have a pointer with both features:

您还可以拥有具有这两个功能的指针:

const int * const ptr3 = &i;

ptr3 = &j; // error
*ptr3 = 7; // error

回答by SridharKritha

Please refer the following link for better understanding about the difference between Const pointer and Pointer on a constant value.

请参阅以下链接以更好地了解常量值上的常量指针和指针之间的区别。

constant pointer vs pointer on a constant value

常量指针与常量值上的指针

回答by user2760375

const int* ptr;here think like *ptr is constant and *ptr can't be change again

const int* ptr;这里认为 *ptr 是恒定的并且 *ptr 不能再次更改

int * const ptr;while here think like ptr as a constant and that can't be change again

int * const ptr;而在这里认为像 ptr 一样是一个常数并且不能再次改变