Page 1 of 1
Help Understanding Pointer Math
Posted: Mon May 17, 2010 8:49 pm
by davidthefat
What is the most common use for pointer math? I understand pointers and stuff, but I see no real applications when it comes to pointer math, except in arrays. In an array, you might want to add, remove and move some data around in that array, so using pointer math, its achieved. But thats the only thing I can think of... I can't ask my AP Comp Sci teacher this, since he didn't use C++ in ages I bet (we learn Java) so any one know?
Re: Help Understanding Pointer Math
Posted: Mon May 17, 2010 9:59 pm
by avansc
i guess you mean pointer arithmetic.
well there are many things you can do really.
note: just make sure you understand the precedence of operators. for instance what *p + 5 vs *(p+5)
for instance this would be a simple example for finding the string length of a c string, ie, char pointer.
int string_length(const char *str)
{
if(!str) return 0;
char *temp = (char*)str;
while(*(++temp));
return temp - str;
}
most of the time pointer arithmetic is used to do some form of search, or comparison based on locality of data.
ps: you can do pointer arithmetic in java, look into the Unsafe class, i think its something like sun.misc.Unsafe. you basically get direct mem access.
I dont know much about it, but im sure its not recomended, you'll prolly crash the jvm if you screw up.
Re: Help Understanding Pointer Math
Posted: Tue May 18, 2010 12:05 pm
by programmerinprogress
Never ask a java teacher a C++ question, especially a one to do with pointers, they'll segfault on you
...And I say this from experience, one of my lecturers hates C++, slags it off all the time in lectures, and I'm thinking to myself "you're java's bitch"
Re: Help Understanding Pointer Math
Posted: Tue May 18, 2010 4:19 pm
by eatcomics
Lol, PiP you should sit there after class and just go on about the wonderful things you've done in c++ and how you have so much control over your programs, and how it doesn't need a VM to run.
Re: Help Understanding Pointer Math
Posted: Wed May 19, 2010 9:36 am
by Genesis
programmerinprogress wrote:Never ask a java teacher a C++ question, especially a one to do with pointers, they'll segfault on you
...And I say this from experience, one of my lecturers hates C++, slags it off all the time in lectures, and I'm thinking to myself "you're java's bitch"
The day one of my lecturer's bad mouthed C++ was the day I lost all hope for the education system.
Re: Help Understanding Pointer Math
Posted: Wed May 19, 2010 9:41 am
by GroundUpEngine
Genesis wrote:programmerinprogress wrote:Never ask a java teacher a C++ question, especially a one to do with pointers, they'll segfault on you
...And I say this from experience, one of my lecturers hates C++, slags it off all the time in lectures, and I'm thinking to myself "you're java's bitch"
The day one of my lecturer's bad mouthed C++ was the day I lost all hope for the education system.
/signed
Re: Help Understanding Pointer Math
Posted: Wed May 19, 2010 10:18 am
by Falco Girgis
Pointer arithmetic is used for some kind of array iteration 99% of the time. There isn't any real magic going on.
Keep this in mind. When I have an array like so:
And I set a pointer equal to the memory address of the first object in the array:
Now incrementing the pointer like so:
Has what effect on the pointer? The memory address of iterator is now equal to object[1] (the second element in the array), NOT the next memory address. Pointer arithmetic causes the pointer to move around in increments of the size of whatever object you are pointing to.