Jump to content

Question about pointers.


Recommended Posts

I tested playing a bit with pointers today and I stumbled on something I can't get my head around.

As you can see in this example, the difference between int pointers is 3 which is I can't find the logic in, and I got even more confused when the difference between double pointers were 2.

#include <iostream>
int main ()
{
    int a=10;
    int b=20;
    int *ptra=&a,*ptrb=&b;
    int diff=ptrb-ptra;
    std::cout << "ptra: " << ptra << "\n" <<
        "ptrb: " << ptrb << "\n" << "Size of pointer: " << sizeof(ptra) << "\n" <<
        "Difference: " << diff << "\nData pointed by ptra: " << *ptra << "\nData pointed by ptrb: " << *ptrb << "\n" <<
        "Data pointed by ptra+difference: " << *(ptra+diff) << "\n";
    return 0;
}

It would be great if someone can explain :)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

You are assuming that a and b are stored next to each other in memory - they might not be. Create an array of 2 ints and do the same things with the first and second element.

Ah I see, now I get a more logical result, 1.

Thanks for the answer! :)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

If you are just starting in on pointers I wish you the best of luck. When I took a data structures course based on c++ the only way I could really wrap my head around pointers was to sit at a white board and draw out what I thought my program was doing line by line (we had to implement our own LinkedList, BTree, etc).

Something ironic though is that using Java has taught me a lot about pointers. Yes I realize that Java doesn't expose pointers directly to the programmer, but while trying to understand the way Java handles method parameters, return values, and garbage collection you end up learning about pointers! And thats not to mention the weird ways Java deals with pointers when using JNI to interface with native code.

Edited by Wus
Link to comment
Share on other sites

Nah I have been using pointers before, it's just that I want to learn more about how the memory works. I also didn't see the use of them before, I was like "Why use pointers when you can just pass the data around in my program" but now I see that it's a real waste to copy the same data around hundreds of times when you can just pass a reference to it :) It becomes even more important when you work with bigger things like classes and stuff.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

<pedantic>

...I see that it's a real waste to copy the same data around hundreds of times when you can just pass a reference to it...

You mean pointer. Given that "reference" has a specific and slightly different meaning, it's important to be explicit.

</pedantic>

Link to comment
Share on other sites

Speaking about reference and pointers, what is the difference between them?

I mean the memory address of the variable is used in both ways:

#include <iostream>
void withptr(int*);
void withref(int&);
int main ()
{
    int a=10;
    int *ptr=&a;
    withptr(ptr);
    std::cout << a << std::endl;
    withref(a);
    std::cout << a << std::endl;
    return 0;
}

void withptr(int *pointer){
    *pointer*=2;
}
void withref(int &ref)
{
    ref*=2;
}

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...