-
making it practical
heya all
this may seem weird but i understand pointer arithmetic etc but i cannot see the practical use(sounds dumb huh)
i'll be cool if someone could probably give me a nice lil example on it so i could be enhanced mentally as a c++ programmer
thanks
zbap
-
for most things you can get by without pointer math. The most straightforward place to use it is with arrays but the compiler has already been developed such that you don't have to do it. Basically:
int array[10];
cout << array[5]; means display the sixth element of the array;
you could get the same thing by doing this:
int *ptr = array;
cout << *(ptr + 5);
but why bother. If you like doing pointer math, so be it. But most of us prefe to use the work arounds already built into the compiler
-
For most programming projects you won't use pointer arithmetic that exceeds the increment operator. There are times where you will find that pointer arithmetic will solve the problem where other notations failed because with something like array notation the compiler must interperet your intentions where with pointer notation you give explicit directions. I've cleared several errors from my code simply by switching from array notation to pointer notation.
-Prelude