Click here to Skip to main content
15,891,850 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If we increment any pointer variable then the poiter points to the next element location , What is that element?
Posted

Depends on what the pointer was addressing in the first place!
If you had an array of integers:
C++
int ar[10];
int* p = &(ar[7]);

then
C++
p++;
and
C++
p = &(ar[8]);
are equivalent.


"but in array we have only 10 location & in case of a variable to which it points, then what will be the next location of pointing and how will it be decided?"


If you mean:
C++
int ar[10];
int* p = &(ar[7]);
p++; //OK, p == &(ar[8])
p++; //OK, p == &(ar[9])
p++; //OK, but p == &(ar[10])
The pointer is now invalid. Provided you do not try to read from it
C++
int i = *p;
or write to it
C++
*p = 6;
then you will not get an error. But it is irrelevant where it is pointing: it is outside the range of valid addresses for the array. Using it will cause problems, either spotted at run time, or corrupting other data and resulting in strange errors.



"so Do you mean that is it a pointer pointing to unknown location but still we can increment it?"


Yes. A pointer is just a number - it has no relevance until it is used. Think about it as an array index in a variable:
C++
int ar[10];
int index = 7;
int i = ar[index];
is fine, no problems.
C++
index = 10;
Is also fine. It's only when you try to use the index that you get problems:
C++
index = 10; // Fine, no hassle.
ar[index] = 6; // And Boom! there is no element 10, so you get problems.
If your pointers were checked when you assigned them, you could not use them effectively: is it invalid to replace a pointer into one array of int values with an address in another array of integers?
 
Share this answer
 
v3
Comments
Ashutosh_g 8-Aug-11 5:21am    
but in array we have only 10 location & in case of a variable to which it points, then what will be the next location of pointing and how will it be decided?
OriginalGriff 8-Aug-11 5:29am    
Answer updated
Ashutosh_g 8-Aug-11 5:33am    
so Do you mean that is it a pointer pointing to unknown location but still we can increment it?
Guyverthree 8-Aug-11 6:20am    
good explanation my 5
Noah Roberts 8-Aug-11 10:57am    
"But it is irrelevant where it is pointing: it is outside the range of valid addresses for the array." <- referring to one past end.

This is not exactly accurate. At best it's misleading, at worse incorrect. The one past end address is quite specifically in the range of valid addresses for an array. It is not, however, a dereferencable one.

This is an important distinction because one past the one past the end as well as non-array pointers that have been incremented are NOT valid addresses in any range. The difference here is that creating them out of valid pointers, incrementing the past-end pointer for example, causes undefined behavior.

Besides that, very good answer.
Incrementing a pointer adds the element size to the address. (The element size is known from the pointer type.) The compiler will not (can not) check if there is data of the expected type at the new address. This is why pointers are so handy and so unsafe in C/C++.

C++
short* p; // p is a pointer to a short (p is not initialized so far)
p++; // now p point two bytes further (wherever that is)
 
Share this answer
 
v5
Comments
Niklas L 8-Aug-11 7:08am    
Short and precise. 5.
The pointer that points the element is actually called the address of that partcular element.
 
Share this answer
 
Comments
Ashutosh_g 8-Aug-11 5:41am    
ok , but when we increment any pointer variable then how it is decided that the pointer will point to the next element and will print the address of the next element?
Richard MacCutchan 8-Aug-11 7:30am    
No it is called a pointer, although it may (and should) hold an address.
For further reading, scroll down to Pointer Arithmetics on this cplusplus.com[^] article on pointers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900