I think what the author was trying to show was that with C your pointers are real pointers to memory and once a variable is set up in a function its memory is fixed. If you fiddle with a pointer to it you will change it. Here is an example:
int *mysub (); main() { int* x; x = mysub(); printf("in main x = %d\n", *x); *x = 5; x = mysub(); } int *mysub () { int z; printf("in sub z = %d\n", z); z = 3; return &z; }
Compiling gives a warning but it still compiles.
$ make temp gcc temp.c -o temp temp.c: In function `mysub': temp.c:13: warning: function returns address of local variable
running it gives you:
$ temp in sub z = 0 in main x = 3 in sub z = 5
as you can see main "reached into" mysub and changed z. There is no equivalent way to "reach into" a perl function and fiddle with its my variables
use strict; my $x; $x = &mysub; printf "in main \$x = %d\n", $$x; $$x = 5; $x = &mysub; sub mysub () { my $z; printf "in sub \$z = %d\n", $z; $z = 3; return \$z; }
outputs:
$ perl temp.pl in sub $z = 0 in main $x = 3 in sub $z = 0
as you can see the main program could not "reach in" and change the value of $z in mysub even though you dereferenced the reference it returned. The my $z in mysub creates a new variable every time and does not just reference a fixed memory location as in C.

Before an expert points it out:

I know, I know, the memory locations may change in C depending on hardware, OS, etc. but I am trying just to give and example of things that are possible in C, not necessarily good programming practice.

Update -- I noticed that if you make z static in mysub

int *mysub () { static int z; ...
the compiler (gcc in my case) will not emit a warning but the output of the program is the same.

--

flounder


In reply to Re: Reference to a function by flounder99
in thread Reference to a function by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.