Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: The Concept of References

by polettix (Vicar)
on Apr 14, 2005 at 17:43 UTC ( [id://447881]=note: print w/replies, xml ) Need Help??


in reply to Re^2: The Concept of References
in thread The Concept of References

One could argue that a pointer in C, e.g. to a struct tm, knows what it points to, so that you can write:
struct tm *p = &data; localtime(p); printf("Year: %d\n", p->tm_year);
which, assuming you have a hash filled with the very same parameters, resembles $p->{'tm_year'} very much.

Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

Don't fool yourself.

Replies are listed 'Best First'.
Re^4: The Concept of References
by revdiablo (Prior) on Apr 14, 2005 at 18:06 UTC

    Correct me if I'm wrong, but I was under the impression that the pointer itself doesn't know whether it's a pointer to a struct, or anything else. The pointer just points to a spot of memory. It's the syntax that you use that determines how the memory gets used.

    Perl's references, on the other hand, know what they're referring to. If you try to use a hash reference as an array reference, perl can look at the reference type and say, "wait a minute, that's not an array reference!"

    I don't know how much difference this makes in terms of usage, but it's certainly a difference between the two constructs.

      No, a pointer in C/C++ is fully typed. The only pointer that really points to a chunk of memory is the void*, but you usually don't want to use it. As a result, it usually happens that you have to typecast a pointer explicitly if you want to make it act as if it was another pointer type:
      int anint; int *p1 = &anint; char *p2 = (char *) p1;

      Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

      Don't fool yourself.
Re^4: The Concept of References
by adrianh (Chancellor) on Apr 15, 2005 at 14:57 UTC
    One could argue that a pointer in C, e.g. to a struct tm, knows what it points to

    In C the programmer tells the pointer what it points to. In Perl the reference knows what it points to. It's an important if subtle difference.

      Eh, not quite. The reference doesn't know. The difference is that in Perl, the thing the references points to, knows what it is. In Perl, SVs are typed.
        The difference is that in Perl, the thing the references points to, knows what it is. In Perl, SVs are typed.

        Point. I should have been more precise.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://447881]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-28 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found