sir can you explain it with example
Addition of an address by a constant integer value i.e. ptr +5 means
address of cell which is 5 * sizeof(*ptr) away from ptr.
sir can you explain it with example
Addition of an address by a constant integer value i.e. ptr +5 means
address of cell which is 5 * sizeof(*ptr) away from ptr.
@Ankit_123
You are right. When we write ptr = ptr + 5 , where ptr is a pointer , it moves 5 steps ahead where each step is of the size = sizeof(*ptr).
Take a look at this little example as I think it might help you out.
#include < iostream >
using namespace std;
int main() {
int A[ ] = {1,2,3,4,5,6,7,8,9,10};
int n = 10;int *p = A; cout << *p << endl; //1 ... as p is at the first location of the array A p = p+2; //Move p two steps ahead , to the box containing data 3 cout << *p << endl; //Prints 3 p = p+5; //p moves 5 steps ahead to the box containing 8. cout << *p << endl; //Prints 8 return 0;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.