Ven*_*h K 116 c pointers const
我想知道它们之间的区别
const int* ptr;
Run Code Online (Sandbox Code Playgroud)
和
int * const ptr;
Run Code Online (Sandbox Code Playgroud)
以及它是如何工作的.
我很难理解或记住这一点.请帮忙.
hac*_*cks 204
const int* ptr;
Run Code Online (Sandbox Code Playgroud)
声明ptr一个指向const int类型的指针.您可以ptr自行修改,但ptr不应修改指向的对象.
const int a = 10;
const int* ptr = &a;
*ptr = 5; // wrong
ptr++; // right
Run Code Online (Sandbox Code Playgroud)
而
int * const ptr;
Run Code Online (Sandbox Code Playgroud)
声明ptr一个const指向int类型的指针.您不能修改ptr但指向的对象ptr.
int a = 10;
int *const ptr = &a;
*ptr = 5; // right
ptr++; // wrong
Run Code Online (Sandbox Code Playgroud)
一般来说,我更喜欢这样的声明,使其易于阅读和理解(从右到左阅读):
int const *ptr; // ptr is a pointer to constant int
int *const ptr; // ptr is a constant pointer to int
Run Code Online (Sandbox Code Playgroud)
Ita*_*chi 22
const int * ptr;
Run Code Online (Sandbox Code Playgroud)
表示指向的数据是常量和不可变的,但指针不是.
int * const ptr;
Run Code Online (Sandbox Code Playgroud)
指的是指针是常量和不可变的,但指针数据不是.
const int* ptr;
Run Code Online (Sandbox Code Playgroud)
是一个指向常量(内容)的指针。您可以修改指针。例如ptr = NULL,ptr++,但无法修改内容。
int * const ptr;
Run Code Online (Sandbox Code Playgroud)
是一个常量指针。相反是可能的。你是不是允许修改指针,但你都可以修改它指向如*ptr += 5。
1)常量指针:这些类型的指针是不能改变它们指向的地址的指针.这意味着假设有一个指向变量的指针(或存储该变量的地址).现在,如果我们尝试将指针指向其他变量(或尝试使指针存储其他变量的地址),那么常量指针就无法实现.
常量指针声明为:( int *const ptr'const'的位置使指针'ptr'成为常量指针)
2)指向常量的指针:这些类型的指针是不能改变它们指向的值的指针.这意味着它们无法更改其所持地址的变量的值.
指向常量的指针声明为:( const int *ptr'const'的位置使指针'ptr'成为指向常量的指针.
例
常量指针
#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;
}
Run Code Online (Sandbox Code Playgroud)
现在,当我们编译上面的代码时,编译器抱怨:
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’
Run Code Online (Sandbox Code Playgroud)
因此我们在上面清楚地看到编译器抱怨我们不能改变常量指针所持有的地址.
指向常量的指针
#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;
}
Run Code Online (Sandbox Code Playgroud)
现在,当编译上面的代码时,编译器会抱怨:
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’
Run Code Online (Sandbox Code Playgroud)
因此,我们在这里也看到编译器不允许指向常量的指针来改变被指向的变量的值.
参考
This Thread
常量指针
让我们首先了解什么是常量指针。常量指针是不能改变其持有地址的指针。换句话说,我们可以说,一旦常量指针指向一个变量,它就不能指向任何其他变量。
常量指针声明如下:
<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;
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中:
指向常量的指针
从名字可以看出,一个不能改变它所指向的变量值的指针被称为常量指针。这些类型的指针可以更改它们指向的地址,但不能更改保存在这些地址处的值。
指向常量的指针被定义为: 定义的
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;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中:
int i;
int j;
int * const ptr1 = &i;
Run Code Online (Sandbox Code Playgroud)
编译器将阻止您更改ptr1.
const int * ptr2 = &i;
Run Code Online (Sandbox Code Playgroud)
编译器将阻止您更改*ptr2.
ptr1 = &j; // error
*ptr1 = 7; // ok
ptr2 = &j; // ok
*ptr2 = 7; // error
Run Code Online (Sandbox Code Playgroud)
请注意,您仍然可以更改*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
Run Code Online (Sandbox Code Playgroud)
您还可以拥有具有这两个功能的指针:
const int * const ptr3 = &i;
ptr3 = &j; // error
*ptr3 = 7; // error
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
205195 次 |
| 最近记录: |