Actually i don't know what is the difference between a normal pointer and a const pointer if i use the below code it will works fine . but when i change int *ptr=#
to int *const ptr = &var1;
then it will not work. can anybody explain what is the difference between a normal pointer and a const pointer ?
实际上我不知道普通指针和const指针的区别是什么如果我使用下面的代码它会很好地工作。但是当我改变int *ptr=&num时;至*const ptr = &var1;那就没用了。有人能解释一下普通指针和常量指针之间的区别吗?
int main(void)
{
int num = 20;
int *ptr = &num ; // if i change to `int *const ptr = &var1;` then it shows some error
*ptr = 20 ; // Valid
ptr ++ ; // valid
return 0;
}
5 个解决方案
#1
5
int* const ptr = &num ;
Will create a constant pointer to an int. The data it points to can be changed, but the pointer it self cannot.
将创建一个指向int的常量指针。它指向的数据可以更改,但是它自己不能。
You cannot change the pointer:
不能更改指针:
ptr++ ;
But you can change the data:
但你可以改变数据:
*ptr = 1234 ;
#2
3
We can do following operations on constant pointers
我们可以对常量指针进行后续操作
- Assigning value at address
- 价值分配地址
- Printing value or address
- 打印值或地址
- Assigning Address at the time of declaration.
- 在声明时分配地址。
We Can't to following operation on constant pointers
我们不能对常量指针进行后续操作
- Adding Integers to Constant pointers.
- 向常量指针添加整数。
- Subtracting integers to constant pointers.
- 将整数减为常数指针。
- Any Operation that can change address of pointer.
- 任何可以改变指针地址的操作。
So, Here in your question..
那么,在你的问题中。
If you declare
如果你声明
int* const ptr = &num ; // this is ok
next line
下一行
*ptr = 20 ; // Assigning value at address this is ok
Now,
现在,
ptr ++ ; // you can not change the value // Error!
Hope it helps!
希望它可以帮助!
#3
1
This:
这样的:
int* const ptr = &num ;
will create a constant pointer to an integer. You can use it to modify the integer's value but you can't change where the pointer points, thus ptr++ ;
is invalid.
将创建一个指向整数的常量指针。你可以用它来修改整数的值但是你不能改变指针的位置,因此ptr++;是无效的。
The const
keyword is usually applied to its left symbol, e.g.
const关键字通常用于它的左符号,例如。
int * const ptr; // A constant pointer (*)
int const * ptr; // A pointer to a constant integer
int const * const ptr; // A constant pointer to a constant integer
const int *ptr; // Shorthand for pointer to a constant integer (equivalent to int const * ptr;)
const
pointers are useful when you want to pass a fixed memory location around and you want to make sure that nobody will modify the pointer's pointed address.
当您希望传递一个固定的内存位置,并且您希望确保没有人会修改指针的指定地址时,const指针是非常有用的。
#4
1
in c, const
is a type qualifier
. use of const
in some variable definition means, the variable will not get modified (will be treated as read-only
)during the entire lifetime of the program.
在c语言中,const是一个类型限定符。在某些变量定义中使用const意味着,在程序的整个生命周期中,变量不会被修改(将被视为只读)。
Usually, when defining a variable / data type with const
, the pratice is to initialize it with required value, as normally, the value it holds cannot be modified at a later part.
通常,在使用const定义变量/数据类型时,实践是使用必需的值初始化它,通常,它所包含的值不能在后面的部分修改。
For example:
例如:
const int a = 10;
const int a = 10;
means, the integer a
will hold the value 10
and it cannot be changed. at a later part,
表示,整数a将保存值10,不能更改。在稍后的部分,
a = 20;
will produce error.
会产生错误。
So, in your case
所以,在你的情况中
int *const ptr = &var;
here, ptr
will always hold the address of var
and it cannot be changed, i.e., we cannot write
在这里,ptr将始终保存var的地址,并且不能更改,即。我们不能写
ptr = &num2; // where num2 is another int, declared like int num2;
it will show compile-time error like :
它将显示编译时错误,如:
error:assignment of read-only variable "*ptr".
错误:为只读变量赋值“*ptr”。
You can find a nice and handy description here.
你可以在这里找到一个很好的描述。
#5
1
int* const pointer = &x ;
it create a constant pointer to an int. The data it points to can be changed, but the pointer it self cannot be changed
它创建一个指向int的常量指针,它指向的数据可以被改变,但是指针本身不能更改。
You cannot change the pointer:
不能更改指针:
pointer++ ;
here you can change the data:
在这里,您可以更改数据:
*pointer=1 ;
#1
5
int* const ptr = &num ;
Will create a constant pointer to an int. The data it points to can be changed, but the pointer it self cannot.
将创建一个指向int的常量指针。它指向的数据可以更改,但是它自己不能。
You cannot change the pointer:
不能更改指针:
ptr++ ;
But you can change the data:
但你可以改变数据:
*ptr = 1234 ;
#2
3
We can do following operations on constant pointers
我们可以对常量指针进行后续操作
- Assigning value at address
- 价值分配地址
- Printing value or address
- 打印值或地址
- Assigning Address at the time of declaration.
- 在声明时分配地址。
We Can't to following operation on constant pointers
我们不能对常量指针进行后续操作
- Adding Integers to Constant pointers.
- 向常量指针添加整数。
- Subtracting integers to constant pointers.
- 将整数减为常数指针。
- Any Operation that can change address of pointer.
- 任何可以改变指针地址的操作。
So, Here in your question..
那么,在你的问题中。
If you declare
如果你声明
int* const ptr = &num ; // this is ok
next line
下一行
*ptr = 20 ; // Assigning value at address this is ok
Now,
现在,
ptr ++ ; // you can not change the value // Error!
Hope it helps!
希望它可以帮助!
#3
1
This:
这样的:
int* const ptr = &num ;
will create a constant pointer to an integer. You can use it to modify the integer's value but you can't change where the pointer points, thus ptr++ ;
is invalid.
将创建一个指向整数的常量指针。你可以用它来修改整数的值但是你不能改变指针的位置,因此ptr++;是无效的。
The const
keyword is usually applied to its left symbol, e.g.
const关键字通常用于它的左符号,例如。
int * const ptr; // A constant pointer (*)
int const * ptr; // A pointer to a constant integer
int const * const ptr; // A constant pointer to a constant integer
const int *ptr; // Shorthand for pointer to a constant integer (equivalent to int const * ptr;)
const
pointers are useful when you want to pass a fixed memory location around and you want to make sure that nobody will modify the pointer's pointed address.
当您希望传递一个固定的内存位置,并且您希望确保没有人会修改指针的指定地址时,const指针是非常有用的。
#4
1
in c, const
is a type qualifier
. use of const
in some variable definition means, the variable will not get modified (will be treated as read-only
)during the entire lifetime of the program.
在c语言中,const是一个类型限定符。在某些变量定义中使用const意味着,在程序的整个生命周期中,变量不会被修改(将被视为只读)。
Usually, when defining a variable / data type with const
, the pratice is to initialize it with required value, as normally, the value it holds cannot be modified at a later part.
通常,在使用const定义变量/数据类型时,实践是使用必需的值初始化它,通常,它所包含的值不能在后面的部分修改。
For example:
例如:
const int a = 10;
const int a = 10;
means, the integer a
will hold the value 10
and it cannot be changed. at a later part,
表示,整数a将保存值10,不能更改。在稍后的部分,
a = 20;
will produce error.
会产生错误。
So, in your case
所以,在你的情况中
int *const ptr = &var;
here, ptr
will always hold the address of var
and it cannot be changed, i.e., we cannot write
在这里,ptr将始终保存var的地址,并且不能更改,即。我们不能写
ptr = &num2; // where num2 is another int, declared like int num2;
it will show compile-time error like :
它将显示编译时错误,如:
error:assignment of read-only variable "*ptr".
错误:为只读变量赋值“*ptr”。
You can find a nice and handy description here.
你可以在这里找到一个很好的描述。
#5
1
int* const pointer = &x ;
it create a constant pointer to an int. The data it points to can be changed, but the pointer it self cannot be changed
它创建一个指向int的常量指针,它指向的数据可以被改变,但是指针本身不能更改。
You cannot change the pointer:
不能更改指针:
pointer++ ;
here you can change the data:
在这里,您可以更改数据:
*pointer=1 ;