Help Understanding Pointer Math

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Help Understanding Pointer Math

Post 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?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Help Understanding Pointer Math

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Help Understanding Pointer Math

Post by programmerinprogress »

Never ask a java teacher a C++ question, especially a one to do with pointers, they'll segfault on you :lol:

...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"
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Help Understanding Pointer Math

Post 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. :lol:
Image
Genesis
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Mon May 17, 2010 9:01 am

Re: Help Understanding Pointer Math

Post by Genesis »

programmerinprogress wrote:Never ask a java teacher a C++ question, especially a one to do with pointers, they'll segfault on you :lol:
...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.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Help Understanding Pointer Math

Post 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 :lol:
...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
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Help Understanding Pointer Math

Post 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:

Code: Select all

Object *object = new Object[5];
And I set a pointer equal to the memory address of the first object in the array:

Code: Select all

Object *iterator = object;
Now incrementing the pointer like so:

Code: Select all

iterator++;
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.
Post Reply