constant pointers

is not the first time I've asked this question...but i hope this time i get a very clear answer once and for all...the difference between constant pointers and general pointers...
i've been told several times that constant pointers dont change??? meaning they point to one thing? but well in this code chkItem is a constant pointer but by moving in the for loop it is changing...so can someone please clarify once and for all? I just taken the habit of using constant pointers all the time i code...but well..is time i really understood this thing..thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
std::vector<int> CreateUniqueVector(const int* firstItem, const int* lastItem)
{
std::vector<int> vReturn;

int dup =  0 ;

	for (const int* curItem = firstItem; curItem != lastItem; ++curItem )
        {
		for (const int* chkItem = firstItem; chkItem != lastItem ; ++chkItem)
			{if ((chkItem != curItem) && (*chkItem == *curItem))
			     {
			       dup ++ ;
			     }
			}
         if (dup > 0)
         counter = dup ;

        //      {
        //       vReturn.push_back(*curItem);
        //      }
      //   else (dup = 0);

        }


 


and one other question ..
am doing this and am getting invalid conversion from int to const * int... mmm an array is a constant pointer no?
1
2
3
4
5
6
7

int main()
{
putinorder(arr[0],arr[49]);

}
void putinorder (const int* pt1, const int* pt2)
Last edited on
const int* pInt;
- Here, the data pointed at by pInt can't be changed once it is set, but you can change the location pInt it pointing at.

int* const pInt;
- Here, the memory-location of pInt can't be changed meaning, pInt++ is illegal, meaning you can't make pInt point to another memory location, but you can change the value holded by the location pointed at by pInt once it's set.

I don't know if this is exactly what you meant, but hope it helps.
Last edited on
For your second question:

What is the type of arr? If it was defined as const int arr[50];, then the expressions arr[0] and arr[49] are both expressions of type const int. The expressions arr, arr+0, arr+49 are of type const int * or in other words pointer to const int.
Last edited on
Jacobhaha...you are the first person explaining it like this..thanks a lot...you hit the nail right on the head without beating about the bush..THANKS A LOT! this will solve a lot of problems..thanks again!!! and thanks pheininger...jacobhaha's explanation made me get everything!!!
closed account (SECMoG1T)
Hi
1
2
const int* chkItem/// is not a constant pointer 
     
this is a pointer to const int ,the pointer to int wont allow you to change the item it points to
Through the pointer But can allow you to change from one const int to another
A const pointer would look like like this [code ]int *const chkitem [/code] for this you can Change the value of the variable it points to but you can't make it point to another variable
Last edited on
I'm glad, good luck :-).
Topic archived. No new replies allowed.