|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
What's the meaning of: int const *pt
Is it equivalent to
const int *pt or int *const pt What's the systematic way to determine the meaning of such expressions (with const qualifier)? Thanks a lot. |
|
#2
|
|||
|
|||
|
Hey a C++ question that I can answer!!! I just read about doing this in my N00b book so I may still be wrong.
Anyway, if I remeber correctly const int *pt can only point to a const integer, while int *const pt is a pointer that only points to one place and can not be changed. I think that is right . |
|
#3
|
|||
|
|||
|
Sounds good to me!
![]() |
|
#4
|
||||
|
||||
|
int const *p is equivalent to const int *p (at least according to my g++ 2.95 compiler). There is a difference between const int *p and int * const p though.
const int *pi * Can be pointed at multiple variables. * Cannot be used to assign a new value to a variable via indirection. int *const ip * Can only be pointed to a single variable (done at initialization) * Can be used to assign a new value to a variable via indirection. const int * const pip (Yes, it is possible to declare a variable of this type too.) * Can only be pointed to a single variable (done at initialization) * Cannot be used to assign a new value via indirection To illustrate the differences: Code:
#include <iostream>
using namespace std;
int main(void) {
int x = 1;
int y = 2;
const int *pi;
pi = &x;
pi = &y; // Can be pointed to another variable
*pi = 23; // <----- NOT ALLOWED
cout << *pi << endl; // <--- Value can be accessed
int * const ip = &x;
*ip = 7; // <----- Can assign a value via indirection
ip = &y; // <----- NOT ALLOWED
cout << *ip << endl; // <---- Value can be accessed
const int * const pip = &y;
*pip = 42; // <------ NOT ALLOWED
pip = &x; // <------ NOT ALLOWED
cout << *pip << endl;
return 0;
}
Hope this helps ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > What's the meaning of: int const *pt |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|