From: Dave
Subject: change a global var from a function?
Date: 
Message-ID: <jzo2b.264015$lK4.8188799@twister1.libero.it>
Hi all.
Is it possible to change a global var. passed as parameter?
Here an example:

>(defvar *something* nil)

>(defun myfunction (globvar)
      (setq globvar t))

>(myfunction *something*)

>(print *something*)

>nil

Why doesn't it change?
In C it's always possible to pass a variable by reference.
I don't know the way to do it (in Lisp).

Thanks to anyone wants to help me..
Dave

From: Joe Marshall
Subject: Re: change a global var from a function?
Date: 
Message-ID: <7k51hhnu.fsf@ccs.neu.edu>
"Dave" <·········@iol.it> writes:

> Hi all.
> Is it possible to change a global var. passed as parameter?
> Here an example:
>
>>(defvar *something* nil)
>
>>(defun myfunction (globvar)
>       (setq globvar t))
>
>>(myfunction *something*)
>
>>(print *something*)
>
>>nil
>
> Why doesn't it change?
> In C it's always possible to pass a variable by reference.

Lisp is a call-by-value language.

> I don't know the way to do it (in Lisp).

There are 3 ways I can think of:

    1.  The value of a global variable is stored in the symbol value
        cell.  Pass the quoted name of the symbol:

        (defun myfunction (symbol)
           (setf (symbol-value symbol) t))

        (myfunction '*something*)

        This trick only works on variables that have been declared or
        declaimed `special'.  It will *not* work in this case:

        (let ((x 22))
          (myfunction 'x)
          (print x)) =>  22

        This is because the value of a lexical variable is not stored
        with the symbol.  This wouldn't work either, and for the same
        reason:

        (let ((var 22))
          (myfunction "var")
          ...)



    2.  Write myfunction as a macro rather than a function:

        (defun mymacro (variable)
           `(SETQ ,variable T))

        (mymacro *something*)  expands to (setq *something* t)

        This *will* work for lexical variables because it is rewriting
        the code at the place you are using it.  On the other hand, it
        won't "work" in a case like this:

        (dolist (name '(*something* *something-else* *another-thing*))
          (mymacro name))

        Because the macro is a simple rewrite:

        (dolist (name '(*something* *something-else* *another-thing*))
          (setq name nil))

        This iterates over the list erasing the iteration variable
        each time around.  Not very useful.



    3.  Pass a `setter' thunk:

        (defun my-function (setter-thunk)
          (funcall setter-thunk t))

        (my-function
           (lambda (new-value) 
              (setq *something* new-value)))
 
        This one is rather complicated, but might be useful in some
        cases.  Think of the lambda expression as a `capability'.  In
        this case, it represents the capability to assign a new value
        to some object.  My-function uses the capability to assign T.

        The disadvantages are obvious:  lots of lambdas and funcalls.

        The advantages are subtle:  you can `customize' the capability.

        (my-function
           (lambda (new-value)
              (when (y-or-n-p "Really set it?")
                 (setq *something* new-value))))

         This lambda expression represents the capability to assign a
         new value, but only with explicit user permission.


Of course you can wrap any of these methods in additional macros to
make them easier to use.
From: Rob Warnock
Subject: Re: change a global var from a function?
Date: 
Message-ID: <BPCdnVcXxu7mZteiXTWc-g@speakeasy.net>
Joe Marshall  <···@ccs.neu.edu> wrote:
+---------------
| "Dave" <·········@iol.it> writes:
| > Is it possible to change a global var. passed as parameter?
...
| Lisp is a call-by-value language.
| 
| > I don't know the way to do it (in Lisp).
| 
| There are 3 ways I can think of:
|     1.  The value of a global variable is stored in the symbol value
|         cell.  Pass the quoted name of the symbol:
...
|     2.  Write myfunction as a macro rather than a function:
...
|     3.  Pass a `setter' thunk:
+---------------

      4. Implement the global variable as a object within which the
         value can be "boxed", rather than as the naked value itself.
         Then you can change a slot in the box, rather than change the
         global variable itself. Obvious candidates for implementing the
         "box" type in Lisp: a cons; a one-word structure; a one-word
         vector, a one-slot class, etc. Leaving cons as an exercise for
         the reader, let's use a structure [note: since we only have
         one slot, a BOA constructor is slightly more convenient to use,
         though slightly more complicated to define]:

	 > (defstruct (box (:constructor make-box (value)))
	     value)

	 BOX
	 > (defun myfunction (box)
	     (setf (box-value box) t))

	 MYFUNCTION
	 > (defvar *something* (make-box nil))

	 *SOMETHING*
	 > (box-value *something*)

	 NIL
         > (myfunction *something*)

	 T
	 > (box-value *something*)

	 T
	 > *something*		; Let's "cheat" and look under the covers...

	 #S(BOX :VALUE T)
         > 

Note that this variation *does* work with lexical variables, too:

	> (let ((x (make-box 'coffee)))
	    (print (box-value x))
	    (myfunction x)
	    (print (box-value x))
	    13)		; Avoid confusing printed things with return values
	 COFFEE 
	 T 
	 13
         > 


-Rob

-----
Rob Warnock, PP-ASEL-IA		<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Rob Warnock
Subject: Re: change a global var from a function?
Date: 
Message-ID: <MfOdnfmX3t8OndaiXTWc-g@speakeasy.net>
Hmmm... I just wrote:
+---------------
| Joe Marshall  <···@ccs.neu.edu> wrote:
| +---------------
| | There are 3 ways I can think of:
| |     1.  The value of a global variable is stored in the symbol value
| |         cell.  Pass the quoted name of the symbol:
| +---------------
| 
|       4. Implement the global variable as a object within which the
|          value can be "boxed", rather than as the naked value itself.
|          Then you can change a slot in the box...
+---------------

I should probably point out that Joe's method #1 is mostly equivalent
to my #4, but using SYMBOL as the "box" type and SYMBOL-VALUE as the
value slot accessor. The main reason for choosing #4 over #1 is that
#4 doesn't require polluting the symbol space of the package you're in.
That is, for every such distinct "box" you want to pass around, #1
requires that you come up with a non-conflicting symbol name; #4 doesn't.

[Also, a symbol is a *big* object compared to a one-slot structure;
even bigger compared to implementing the "box" type as a CONS (that
"exercise for the reader" left undone in my previous)...]


-Rob

-----
Rob Warnock, PP-ASEL-IA		<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Kalle Olavi Niemitalo
Subject: Re: change a global var from a function?
Date: 
Message-ID: <87smnodytf.fsf@Astalo.kon.iki.fi>
····@rpw3.org (Rob Warnock) writes:

> That is, for every such distinct "box" you want to pass around,
> #1 requires that you come up with a non-conflicting symbol name

You could avoid this with uninterned symbols.

  (defun indirect-swap (box1 box2)
    (rotatef (symbol-value box1)
             (symbol-value box2)))

  (let ((left (gensym))
        (right (gensym)))
    (setf (symbol-value left) "red")
    (setf (symbol-value right) "hat")
    (indirect-swap left right)
    (concatenate 'string (symbol-value left) (symbol-value right)))

If the code needn't be reentrant or pronounceable, you could even
use '#:|| instead of (gensym).

> [Also, a symbol is a *big* object compared to a one-slot structure;
> even bigger compared to implementing the "box" type as a CONS (that
> "exercise for the reader" left undone in my previous)...]

Can't argue against that.
From: Kaz Kylheku
Subject: Re: change a global var from a function?
Date: 
Message-ID: <cf333042.0308251109.7eb4ebcd@posting.google.com>
"Dave" <·········@iol.it> wrote in message news:<························@twister1.libero.it>...
> Hi all.
> Is it possible to change a global var. passed as parameter?
> Here an example:
> 
> >(defvar *something* nil)
>  
> >(defun myfunction (globvar)
>       (setq globvar t))
> 
> >(myfunction *something*)
>  
> >(print *something*)
>  
> >nil
> 
> Why doesn't it change?
> In C it's always possible to pass a variable by reference.

Maybe in some parallel universe with its own flavor of the C language.
In this nook of the five-dimensional manifold, the C language
simulates reference semantics using explicit pointers. You must
explicitly lift the address of a scalar variable, and pass the
pointer, which must be explicitly dereferenced. (It's possible to
implicitly take the address of an array element).

And not all objects can have a pointer aimed at them, only ones that
are lvalues. For example.

    struct foo {
      int x[3];
    };

    struct foo foo_returning_func();

    /*...*/

    foo_returning_func().x[3]; /* error, .x is not an lvalue */

The array indexing operator implicitly takes the address of the first
element of the array, performs pointer displacement and dereferences
it. Therefore it requires an array which is an lvalue. The array here
is embedded in a struct object which is just the return value of a
function, and not an lvalue. This hole in the C language actually
prevents you from using the array directly; you must assign the struct
to a compatible object and use the copy.

In C, a variable is just a name given to a memory location. If that
memory location is ordinary storage, rather than the non-lvalue result
of an expression like the struct in the above example, then you can
create a pointer to that location, and bypass the variable reference.
The pointer is in effect a run-time name of the location that can be
used as an alternative to the symbolic reference, and is itself a
run-time value that can be manipulated in various ways.

Lisp variables are not names of memory locations that can have
pointers aimed at them. They are abstract associations (bindings)
between symbols and values, relative to some context, known as an
environment.

You can't take the address of a lexical variable at all. As far as
special variables are concerned (that which you are calling global),
you can do indirection upon them by passing the symbol itself. This is
the closest analogy to the ``address of'' operator in the C language.
The following table may be helpful:

    C example                         Lisp example

    #include <stdio.h>                (defvar *global* 0)

    int global;                       (defun use-value-only (value)
                                        (print value))
    void use_value_only(int value)
    {                                 (defun modify-global (symbol)
      printf("%d\n", value);            (setf (symbol-value symbol)
    }                                          42)) 

    void modify_global(int *ptr_glob) (use-value-only *global*)
    {                                 (modify-global '*global*)
      *ptr_glob = 42;                 (use-value-only *global*)
    }

    int main(void)
    {
      use_value_only(global);
      modify_global(&global);
      use_value_only(global);
      return 0;
    }

In the Lisp program, the symbol *global* is quoted, so that you pass
the symbol rather than the value of the binding which it has in the
given environment---its dynamic, global binding.

In the C program, the variable global is also quoted, in a way, using
the unary & operator. This is needed so that a pointer to the location
is passed instead of the value produced by evaluating that location.
From: Frode Vatvedt Fjeld
Subject: Re: change a global var from a function?
Date: 
Message-ID: <2hvfslzv9u.fsf@vserver.cs.uit.no>
"Dave" <·········@iol.it> writes:

> Is it possible to change a global var. passed as parameter?
> Here an example:
>
>>(defvar *something* nil)
>
>>(defun myfunction (globvar)
>       (setq globvar t))
>
>>(myfunction *something*)
>
>>(print *something*)

Try this:

  (defvar *something*)
  (defun my-function (variable-name)
    (check-type variable-name symbol)
    (warn "Setting the value of the variable ~S." variable-name)
    (setf (symbol-value variable-name) t))
  (my-function '*something*)
  *something* => t

This is not something you should expect to do very often.

-- 
Frode Vatvedt Fjeld
From: Coby Beck
Subject: Re: change a global var from a function?
Date: 
Message-ID: <bif416$h73$1@otis.netspace.net.au>
"Frode Vatvedt Fjeld" <······@cs.uit.no> wrote in message
···················@vserver.cs.uit.no...
> "Dave" <·········@iol.it> writes:
>
> > Is it possible to change a global var. passed as parameter?
> > Here an example:
> >
> >>(defvar *something* nil)
> >
> >>(defun myfunction (globvar)
> >       (setq globvar t))
> >
> >>(myfunction *something*)
> >
> >>(print *something*)
>
> Try this:
>
>   (defvar *something*)
>   (defun my-function (variable-name)
>     (check-type variable-name symbol)
>     (warn "Setting the value of the variable ~S." variable-name)
>     (setf (symbol-value variable-name) t))
>   (my-function '*something*)
>   *something* => t
>
> This is not something you should expect to do very often.

And as such, I think it is rather bad advice to a novice.  The above
function certainly shows knowledge of lisp and an example of lisp's
flexibility but unless you already know how to do that, you definately don't
want to.

-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")
From: Jacek Generowicz
Subject: Re: change a global var from a function?
Date: 
Message-ID: <tyfad9xakie.fsf@pcepsft001.cern.ch>
"Dave" <·········@iol.it> writes:

> Hi all.
> Is it possible to change a global var. passed as parameter?
> Here an example:
> 
> >(defvar *something* nil)
> 
> >(defun myfunction (globvar)
>       (setq globvar t))
> 
> >(myfunction *something*)
> 
> >(print *something*)
> 
> >nil
> 
> Why doesn't it change?
>
> In C it's always possible to pass a variable by reference.
> I don't know the way to do it (in Lisp).

Lisp passes by value ... but the value is usually[*] a reference.

Try

  (setq *something* (list 1 2 3 4))  ; assuming you've already done (defvar *something* ...)

  (defun hisfunction (x)
    (setf (car x) 9))

  (hisfunction *something*)

  (print *something*)   =>  (9 2 3 4)

Think about this, and ask again. (There's no point in writing reams
and reams about this, without getting some feedback from you.)


[*] Fixnums and characters are the exceptions.


PS. Dynamic variables (what you get from a defvar) behave differently
from C globals, but that's not directly relevant to your question.
From: Sam Zoghaib
Subject: Re: change a global var from a function?
Date: 
Message-ID: <bidh8a$tom$1@news-reader3.wanadoo.fr>
Dave wrote in article <························@twister1.libero.it> on Monday
25 August 2003 15:55 in comp.lang.lisp:

> Why doesn't it change?
> In C it's always possible to pass a variable by reference.
> I don't know the way to do it (in Lisp).
> 

Others have given good answers to your question, but I'd like to add that your
statement about being able to pass by reference in C is false.
C always passes variables by values; to modify an argument, you pass a pointer
to the variable that you want to modify, and modify what that pointer points
to. But that's still passing by value; you pass the *value* of the pointer,
ie, the address of the variable; you cannot, for instance, modify that
address in that function (ie modify the pointer itself).

Sam
-- 
"So if you meet me, have some courtesy, have some sympathy, and some taste
 Use all your well-learned politesse, or I'll lay your soul to waste"

    - The Rolling Stones, "Sympathy for the Devil"
From: Justin Dubs
Subject: Re: change a global var from a function?
Date: 
Message-ID: <2e262238.0308251406.174c0555@posting.google.com>
Sam Zoghaib <·····@zep.homedns.orgSPAM> wrote in message news:<············@news-reader3.wanadoo.fr>...
> Dave wrote in article <························@twister1.libero.it> on Monday
> 25 August 2003 15:55 in comp.lang.lisp:
> 
> > Why doesn't it change?
> > In C it's always possible to pass a variable by reference.
> > I don't know the way to do it (in Lisp).
> > 
> 
> Others have given good answers to your question, but I'd like to add that your
> statement about being able to pass by reference in C is false.

I assume he was referring to the C++ notion of pass-by-reference.  As
in:

void foo(int &bar) {
    bar = 42;
}

A lot of C++ users who simply view C++ as an OO extension of C forget
that things like this are not backwards-compatible.

> C always passes variables by values; to modify an argument, you pass a pointer
> to the variable that you want to modify, and modify what that pointer points
> to. But that's still passing by value; you pass the *value* of the pointer,
> ie, the address of the variable; you cannot, for instance, modify that
> address in that function (ie modify the pointer itself).

Well, you /can/ modify the pointer in the function, but it'll just
only modify the local copy.  Like you said, it's pass by value.  You
can always just pass a double-pointer instead, if modifying the
original pointer is your goal.

Justin Dubs
From: Sam Zoghaib
Subject: Re: change a global var from a function?
Date: 
Message-ID: <bie7pu$s4c$1@news-reader2.wanadoo.fr>
Justin Dubs wrote in article <····························@posting.google.com>
on Tuesday 26 August 2003 00:06 in comp.lang.lisp:

>> Others have given good answers to your question, but I'd like to add that
>> your statement about being able to pass by reference in C is false.
> 
> I assume he was referring to the C++ notion of pass-by-reference.

Perhaps, but there is no way I could have known that.

> A lot of C++ users who simply view C++ as an OO extension of C forget
> that things like this are not backwards-compatible.

Too bad for them...

>> C always passes variables by values; to modify an argument, you pass a
>> pointer to the variable that you want to modify, and modify what that
>> pointer points to. But that's still passing by value; you pass the *value*
>> of the pointer, ie, the address of the variable; you cannot, for instance,
>> modify that address in that function (ie modify the pointer itself).
> 
> Well, you /can/ modify the pointer in the function, but it'll just
> only modify the local copy. 

Indeed; that's what I meant; if that was not clear, sorry.

Sam
-- 
"Fear is the path to the dark side.
 Fear leads to anger, anger leads to hatred, hatred leads to suffering.
 I sense much fear in you."
From: Barry Margolin
Subject: Re: change a global var from a function?
Date: 
Message-ID: <OWs2b.397$mD.137@news.level3.com>
In article <············@news-reader3.wanadoo.fr>,
Sam Zoghaib  <·····@zep.homedns.orgSPAM> wrote:
>Dave wrote in article <························@twister1.libero.it> on Monday
>25 August 2003 15:55 in comp.lang.lisp:
>
>> Why doesn't it change?
>> In C it's always possible to pass a variable by reference.
>> I don't know the way to do it (in Lisp).
>> 
>
>Others have given good answers to your question, but I'd like to add that your
>statement about being able to pass by reference in C is false.

Perhaps he was confusing C with C++; the latter does allow passing by
reference.

-- 
Barry Margolin, ··············@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Dave
Subject: Re: change a global var from a function?
Date: 
Message-ID: <Kmt2b.264700$lK4.8212721@twister1.libero.it>
"Sam Zoghaib" <·····@zep.homedns.orgSPAM>

>...[cut]
>...
> C always passes variables by values; to modify an argument, you pass a pointer
> to the variable that you want to modify, and modify what that pointer points
> to. But that's still passing by value; you pass the *value* of the pointer,
> ie, the address of the variable; you cannot, for instance, modify that
> address in that function (ie modify the pointer itself).
> that's still passing by value; you pass the *value* of the pointer,
> ie, the address of the variable; you cannot, for instance, modify that
> address in that function (ie modify the pointer itself).

The C allows to make whichever thing.., however,
the address of the variable normally should be named "pass-by-reference".
"pass-by-value" data types enjoy a simple one-to-one relationship between variables and their contents, "pass-by-reference" data types identify a reference point to their contents.

Best regards.
Dave
From: Sam Zoghaib
Subject: Re: change a global var from a function?
Date: 
Message-ID: <bidqfi$9b4$1@news-reader2.wanadoo.fr>
Dave wrote in article <························@twister1.libero.it> on Monday
25 August 2003 21:23 in comp.lang.lisp:

> The C allows to make whichever thing.., however,
> the address of the variable normally should be named "pass-by-reference".

No; again, even if you pass the address of the variable, you won't be able to
modify that address itself. You'll only be able to modify what is stored at
that address.

From K&R 2nd edition, p. 95:

"Since C passes arguments to functions by value, there is no direct way for
the called function to alter an argument in the calling function"

It is not a matter of name; passing by reference a variable and passing a
pointer to that variable in a pass-by-value system are two different things. 

Sam
-- 
"[...] but the delight and pride of Aule is in the deed of making, and in the
thing made, and neither in possession nor in his own mastery; wherefore he
gives and hoards not, and is free from care, passing ever on to some new
work."

    - J.R.R. Tolkien, Ainulindale (Silmarillion)
From: Dave
Subject: Re: change a global var from a function?
Date: 
Message-ID: <jhL2b.266576$lK4.8273725@twister1.libero.it>
"Sam Zoghaib" <·····@zep.homedns.orgSPAM> ha scritto nel messaggio ·················@news-
> 
> "Since C passes arguments to functions by value, there is no direct way for
> the called function to alter an argument in the calling function"
> 
> It is not a matter of name; passing by reference a variable and passing a
> pointer to that variable in a pass-by-value system are two different things. 
> 

void myfunc(char *pstr, char **cstr) {
    printf(pstr);
    *cstr = ++pstr;   //direct way to alter (*pstr) argument!!
    printf(pstr);
}

int main(int argc, char *argv[]) {
    char *str = "Hello, world!\n";
    myfunc(str, &str);
    printf(str);
    return 0;
}

Dave
From: Joe Marshall
Subject: Re: change a global var from a function?
Date: 
Message-ID: <smnodxpj.fsf@ccs.neu.edu>
"Dave" <·········@iol.it> writes:

> "Sam Zoghaib" <·····@zep.homedns.orgSPAM> ha scritto nel messaggio ·················@news-
>> 
>> "Since C passes arguments to functions by value, there is no direct way for
>> the called function to alter an argument in the calling function"
>> 
>> It is not a matter of name; passing by reference a variable and passing a
>> pointer to that variable in a pass-by-value system are two different things. 
>> 
>
> void myfunc(char *pstr, char **cstr) {
>     printf(pstr);
>     *cstr = ++pstr;   //direct way to alter (*pstr) argument!!
>     printf(pstr);
> }
>
> int main(int argc, char *argv[]) {
>     char *str = "Hello, world!\n";
>     myfunc(str, &str);
>     printf(str);
>     return 0;
> }

With call-by-value semantics, the following code cannot
do anything but print 42:

int main (int argc, char * argv[]) 
{
    int b = 42;
    any_function_at_all (b);
    printf ("%d", b);
    return 0;
}

Sure you *can* pass references, and aggregate structures, but
call-by-value means that if you *didn't*, the callee has no recourse.
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <ly3cfntiqt.fsf@cartan.de>
Joe Marshall <···@ccs.neu.edu> writes:

> With call-by-value semantics, the following code cannot do anything
> but print 42:
> 
> int main (int argc, char * argv[]) 
> {
>     int b = 42;
>     any_function_at_all (b);
>     printf ("%d", b);
>     return 0;
> }
> 
> Sure you *can* pass references, and aggregate structures, but
> call-by-value means that if you *didn't*, the callee has no
> recourse.

In a powerful language like C, anything is possible:

If this is boom.c:

#include <stdio.h>

static void any_function_at_all(int b)
{
	int *blark = &b;

	blark[1] = 17;
}

int main(void)
{
    int b = 42;

    any_function_at_all(b);
    printf("%d\n", b);
    return 0;
}

I get

····@pc022[boom]$ gcc -Wall boom.c
····@pc022[boom]$ ./a.out 
17
····@pc022[boom]$ 

Oops!

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Fred Gilham
Subject: Re: change a global var from a function?
Date: 
Message-ID: <u7k78z6u7e.fsf@snapdragon.csl.sri.com>
Nils Goesche <······@cartan.de> writes:
> 
> In a powerful language like C, anything is possible:
> 
> If this is boom.c:
> 
> #include <stdio.h>
> 
> static void any_function_at_all(int b)
> {
> 	int *blark = &b;
> 
> 	blark[1] = 17;
> }
> 
> int main(void)
> {
>     int b = 42;
> 
>     any_function_at_all(b);
>     printf("%d\n", b);
>     return 0;
> }
> 
> I get
> 
> ····@pc022[boom]$ gcc -Wall boom.c
> ····@pc022[boom]$ ./a.out 
> 17
> ····@pc022[boom]$ 
> 
> Oops!

Bad!  Naughty!!!  (Newbies, cover your eyes!  DON'T look!  You do NOT
need to know this!)

Besides, it doesn't always work.

boomerang:~ > cat boom.c

#include <stdio.h>

static void any_function_at_all(int b)
{
        int *blark = &b;

        blark[1] = 17;
}

int main(void)
{
    int b = 42;

    any_function_at_all(b);
    printf("%d\n", b);
    return 0;
}
boomerang:~ > gcc -Wall boom.c
boomerang:~ > ./a.out
42
boomerang:~ >

Righteousness prevails!

-- 
Fred Gilham                                        ······@csl.sri.com
Propaganda makes up our mind for us, but in such a way that it leaves
us the sense of pride and satisfaction of men who have made up their
own minds. And in the last analysis, propaganda achieves this effect
because we want it to. This is one of the few real pleasures left to
modern man: this illusion that he is thinking for himself when, in
fact, someone else is doing his thinking for him.  - Thomas Merton
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <lylltfrvpe.fsf@cartan.de>
Fred Gilham <······@snapdragon.csl.sri.com> writes:

> Nils Goesche <······@cartan.de> writes:
> > 
> > I get
> > 
> > ····@pc022[boom]$ gcc -Wall boom.c
> > ····@pc022[boom]$ ./a.out 
> > 17
> > ····@pc022[boom]$ 

> Besides, it doesn't always work.
> 
> boomerang:~ > gcc -Wall boom.c
> boomerang:~ > ./a.out
> 42
> boomerang:~ >
> 
> Righteousness prevails!

Nah, your gcc installation must be broken :-)

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: ·············@comcast.net
Subject: Re: change a global var from a function?
Date: 
Message-ID: <lltfz0e7.fsf@comcast.net>
Nils Goesche <······@cartan.de> writes:

> Joe Marshall <···@ccs.neu.edu> writes:
>
>> With call-by-value semantics, the following code cannot do anything
>> but print 42:
>> 
>> int main (int argc, char * argv[]) 
>> {
>>     int b = 42;
>>     any_function_at_all (b);
>>     printf ("%d", b);
>>     return 0;
>> }
>> 
>> Sure you *can* pass references, and aggregate structures, but
>> call-by-value means that if you *didn't*, the callee has no
>> recourse.
>
> In a powerful language like C, anything is possible:
>
> If this is boom.c:
>
> #include <stdio.h>
>
> static void any_function_at_all(int b)
> {
> 	int *blark = &b;
>
> 	blark[1] = 17;
> }
>
> int main(void)
> {
>     int b = 42;
>
>     any_function_at_all(b);
>     printf("%d\n", b);
>     return 0;
> }
>
> Oops!

Oops is right.  That's not legal C, and you're lucky you only got
bizarre semantics.  You could have had monkey fly out of your nose.
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <lyy8xfrz3k.fsf@cartan.de>
·············@comcast.net writes:

> Nils Goesche <······@cartan.de> writes:
> 
> > In a powerful language like C, anything is possible:
> >
> > If this is boom.c:
> >
> > #include <stdio.h>
> >
> > static void any_function_at_all(int b)
> > {
> > 	int *blark = &b;
> >
> > 	blark[1] = 17;
> > }
> >
> > int main(void)
> > {
> >     int b = 42;
> >
> >     any_function_at_all(b);
> >     printf("%d\n", b);
> >     return 0;
> > }
> >
> > Oops!
> 
> Oops is right.  That's not legal C, and you're lucky you only got
> bizarre semantics.  You could have had monkey fly out of your nose.

Bah, pusillanimous pedantry.  Every Real Programmer knows you can't
write real world programs in pure ANSI C.  That's why most C programs
stop working unless you use the right version of the compiler and
don't change the optimization settings...

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Hartmann Schaffer
Subject: Re: change a global var from a function?
Date: 
Message-ID: <3f4d2e77@news.sentex.net>
In article <············@comcast.net>,
	·············@comcast.net writes:
> Nils Goesche <······@cartan.de> writes:
> 
>> Joe Marshall <···@ccs.neu.edu> writes:
>>
>>> With call-by-value semantics, the following code cannot do anything
>>> but print 42:
>>> 
>>> int main (int argc, char * argv[]) 
>>> {
>>>     int b = 42;
>>>     any_function_at_all (b);
>>>     printf ("%d", b);
>>>     return 0;
>>> }
>>> 
>>> Sure you *can* pass references, and aggregate structures, but
>>> call-by-value means that if you *didn't*, the callee has no
>>> recourse.
>>
>> In a powerful language like C, anything is possible:
>>
>> If this is boom.c:
>>
>> #include <stdio.h>
>>
>> static void any_function_at_all(int b)
>> {
>> 	int *blark = &b;
>>
>> 	blark[1] = 17;
>> }
>>
>> int main(void)
>> {
>>     int b = 42;
>>
>>     any_function_at_all(b);
>>     printf("%d\n", b);
>>     return 0;
>> }
>>
>> Oops!
> 
> Oops is right.  That's not legal C, and you're lucky you only got
> bizarre semantics.  You could have had monkey fly out of your nose.

it is legal, but won't have the effect nils pretended it should

hs

-- 

ceterum censeo SCO esse delendam
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <87ad9u3f45.fsf@darkstar.cartan>
··@heaven.nirvananet (Hartmann Schaffer) writes:

> In article <············@comcast.net>,
> 	·············@comcast.net writes:
> > Nils Goesche <······@cartan.de> writes:
> > 
> >> In a powerful language like C, anything is possible:
> >>
> >> If this is boom.c:
> >>
> >> #include <stdio.h>
> >>
> >> static void any_function_at_all(int b)
> >> {
> >> 	int *blark = &b;
> >>
> >> 	blark[1] = 17;
> >> }
> >>
> >> int main(void)
> >> {
> >>     int b = 42;
> >>
> >>     any_function_at_all(b);
> >>     printf("%d\n", b);
> >>     return 0;
> >> }
> >>
> >> Oops!
> > 
> > Oops is right.  That's not legal C, and you're lucky you only got
> > bizarre semantics.  You could have had monkey fly out of your nose.
> 
> it is legal, but won't have the effect nils pretended it should

Actually, it's the other way around: It is not legal, but might
very well have the effect I said it would (see my reply to
Mr. Mommer for an explanation :-) (blark[0] = 17; would be legal.
No other index.)

Regards,
-- 
Nils G�sche
Mercy to the guilty is cruelty to the innocent.

PGP key ID #xD26EF2A0
From: Hartmann Schaffer
Subject: Re: change a global var from a function?
Date: 
Message-ID: <3f504e6b@news.sentex.net>
In article <··············@darkstar.cartan>,
	Nils Goesche <···@cartan.de> writes:
> ··@heaven.nirvananet (Hartmann Schaffer) writes:
> 
>> In article <············@comcast.net>,
>> 	·············@comcast.net writes:
>> > Nils Goesche <······@cartan.de> writes:
>> > 
>> >> In a powerful language like C, anything is possible:
>> >>
>> >> If this is boom.c:
>> >>
>> >> #include <stdio.h>
>> >>
>> >> static void any_function_at_all(int b)
>> >> {
>> >> 	int *blark = &b;
>> >>
>> >> 	blark[1] = 17;
>> >> }
>> >>
>> >> int main(void)
>> >> {
>> >>     int b = 42;
>> >>
>> >>     any_function_at_all(b);
>> >>     printf("%d\n", b);
>> >>     return 0;
>> >> }
>> >>
>> >> Oops!
>> > 
>> > Oops is right.  That's not legal C, and you're lucky you only got
>> > bizarre semantics.  You could have had monkey fly out of your nose.
>> 
>> it is legal, but won't have the effect nils pretended it should
> 
> Actually, it's the other way around: It is not legal, but might
> very well have the effect I said it would (see my reply to
> Mr. Mommer for an explanation :-) (blark[0] = 17; would be legal.
> No other index.)

oops.  i didn't look closely enough and assumed a char *blank

hs

-- 

ceterum censeo SCO esse delendam
From: Sam Zoghaib
Subject: Re: change a global var from a function?
Date: 
Message-ID: <biiclk$3i9$1@news-reader2.wanadoo.fr>
Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
August 2003 13:58 in comp.lang.lisp:

> In a powerful language like C, anything is possible:
> 
> If this is boom.c:
> 
> #include <stdio.h>
> 
> static void any_function_at_all(int b)
> {
> int *blark = &b;
> 
> blark[1] = 17;
> }

This is undefined behavior; you are dereferencing a pointer that points to an 
uninitialized memory location. The program may crash, run properly, turn your
dog into a cat, or anything else.

> int main(void)
> {
>     int b = 42;
> 
>     any_function_at_all(b);
>     printf("%d\n", b);
>     return 0;
> }
> 
> I get
> 
> ····@pc022[boom]$ gcc -Wall boom.c
> ····@pc022[boom]$ ./a.out
> 17
> ····@pc022[boom]$
> 
Running that same code on my box prints 42, not 17.

I never said you cannot change an argument. I said that C passes by value;
that means it's actually a copy of the object that is passed, not the object
itself. No matter what you do or say, it stays that way. You can obviously
pass a pointer to the object so that you can modify it (retaining the
modification outside the scope of the called function). That is *not* call by
reference.

Sam
-- 
"People sometimes ask me if it is a sin in the Church of Emacs to use vi.
 Using a free version of vi is not a sin; it's a penance".

        - Richard Stallman
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <lyu183ryxb.fsf@cartan.de>
Sam Zoghaib <·····@zep.homedns.orgSPAM> writes:

> Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
> August 2003 13:58 in comp.lang.lisp:
> 
> > In a powerful language like C, anything is possible:
> > 
> > If this is boom.c:
> > 
> > #include <stdio.h>
> > 
> > static void any_function_at_all(int b)
> > {
> > int *blark = &b;
> > 
> > blark[1] = 17;
> > }
> 
> This is undefined behavior; you are dereferencing a pointer that
> points to an uninitialized memory location. The program may crash,
> run properly, turn your dog into a cat, or anything else.

Thanks for enlightening me.  I'd never have thought of that.  There I
was, happily using call-by-reference in all my C code, and now it's
all over.  *sniff*

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Sam Zoghaib
Subject: Re: change a global var from a function?
Date: 
Message-ID: <biikh1$l0o$1@news-reader5.wanadoo.fr>
Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
August 2003 15:51 in comp.lang.lisp:


> Thanks for enlightening me.  I'd never have thought of that.  There I
> was, happily using call-by-reference in all my C code, and now it's
> all over.  *sniff*

Considering how you do what you wrongly call "call by refernce", your
statement is similar to: "I was happily dereferencing null pointers; now it's
all over". Let me answer: "I hope so".

Sam
-- 
"So if you meet me, have some courtesy, have some sympathy, and some taste
 Use all your well-learned politesse, or I'll lay your soul to waste"

    - The Rolling Stones, "Sympathy for the Devil"
From: Jacek Generowicz
Subject: Re: change a global var from a function?
Date: 
Message-ID: <tyf1xv642ta.fsf@pcepsft001.cern.ch>
Sam Zoghaib <·····@zep.homedns.orgSPAM> writes:

> Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
> August 2003 15:51 in comp.lang.lisp:
> 
> 
> > Thanks for enlightening me.  I'd never have thought of that.  There I
> > was, happily using call-by-reference in all my C code, and now it's
> > all over.  *sniff*
> 
> Considering how you do what you wrongly call "call by refernce", your
> statement is similar to: "I was happily dereferencing null pointers; now it's
> all over". Let me answer: "I hope so".

Hint: Sam, look up "irony" in the dictionary.
From: Hartmann Schaffer
Subject: Re: change a global var from a function?
Date: 
Message-ID: <3f4d2f64@news.sentex.net>
In article <············@news-reader2.wanadoo.fr>,
	Sam Zoghaib <·····@zep.homedns.orgSPAM> writes:
> Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
> August 2003 13:58 in comp.lang.lisp:
> 
>> In a powerful language like C, anything is possible:
>> 
>> If this is boom.c:
>> 
>> #include <stdio.h>
>> 
>> static void any_function_at_all(int b)
>> {
>> int *blark = &b;
>> 
>> blark[1] = 17;
>> }
> 
> This is undefined behavior; you are dereferencing a pointer that points to an 
> uninitialized memory location. The program may crash, run properly, turn your
> dog into a cat, or anything else.

wrong.  the memory location is initialized to whatever gets passed as
argument to the function

> ...

hs

-- 

ceterum censeo SCO esse delendam
From: Daniel Barlow
Subject: Re: change a global var from a function?
Date: 
Message-ID: <87vfsi51lc.fsf@noetbook.telent.net>
Sam Zoghaib <·····@zep.homedns.orgSPAM> writes:

> points to an uninitialized memory location. The program may crash,
> run properly, turn your dog into a cat, or anything else.

It turned me into a newt.


-dan

-- 

   http://www.cliki.net/ - Link farm for free CL-on-Unix resources 
From: Matthew Danish
Subject: Re: change a global var from a function?
Date: 
Message-ID: <20030827225843.GK1454@mapcar.org>
On Wed, Aug 27, 2003 at 08:42:39PM +0100, Daniel Barlow wrote:
> It turned me into a newt.

A newt!?

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
From: Fred Gilham
Subject: Re: change a global var from a function?
Date: 
Message-ID: <u7n0dtzo74.fsf@snapdragon.csl.sri.com>
Matthew Danish <·······@andrew.cmu.edu> writes:

> On Wed, Aug 27, 2003 at 08:42:39PM +0100, Daniel Barlow wrote:
> > It turned me into a newt.
> 
> A newt!?

He got better.

-- 
Fred Gilham                                       ······@csl.sri.com
...every candid Reader will easily understand my Discourse to be
intended only in Defence of *nominal* Christianity, the other having
been for some time wholly laid aside by general Consent, as utterly
inconsistent with all our present Schemes of Wealth and Power.
                                                 --- Jonathan Swift
From: Marco Antoniotti
Subject: Re: change a global var from a function?
Date: 
Message-ID: <3F4F721A.1080409@cs.nyu.edu>
Matthew Danish wrote:
> On Wed, Aug 27, 2003 at 08:42:39PM +0100, Daniel Barlow wrote:
> 
>>It turned me into a newt.
> 
> 
> A newt!?
> 

It could have been worse.  He could have been turned into a gingrich :)

Cheers

--
Marco
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <lyznhspiyn.fsf@cartan.de>
Marco Antoniotti <·······@cs.nyu.edu> writes:

> Matthew Danish wrote:
> > On Wed, Aug 27, 2003 at 08:42:39PM +0100, Daniel Barlow wrote:
> >
> >>It turned me into a newt.
> > A newt!?
> >
> 
> It could have been worse.  He could have been turned into a gingrich :)

Damn, when I find the bastard who leaked my fiendish plan he'd better
run fast...

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <87n0du3ipz.fsf@darkstar.cartan>
Daniel Barlow <···@telent.net> writes:

> Sam Zoghaib <·····@zep.homedns.orgSPAM> writes:
> 
> > points to an uninitialized memory location. The program may crash,
> > run properly, turn your dog into a cat, or anything else.
> 
> It turned me into a newt.

We are sorry to hear that.  Please note that the license
agreement you signed explicitly excluded any kind of warranty
from our side.  But our next release might turn you into
something more convenient, so please upgrade when it's ready.

Oog,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID #xD26EF2A0
From: Mario S. Mommer
Subject: Re: change a global var from a function?
Date: 
Message-ID: <fzoeyb40x5.fsf@cupid.igpm.rwth-aachen.de>
Nils Goesche <······@cartan.de> writes:
> 	blark[1] = 17;
             ^^^
Shouldn't it be 0?
From: Fred Gilham
Subject: Re: change a global var from a function?
Date: 
Message-ID: <u7smnmzt0t.fsf@snapdragon.csl.sri.com>
Mario S. Mommer <········@yahoo.com> writes:

> Nils Goesche <······@cartan.de> writes:
> > 	blark[1] = 17;
>              ^^^
> Shouldn't it be 0?

No.  Nils Goesche is summoning the powers of evil here.

If it works on your system, see an exorcist immediately.

-- 
Fred Gilham                                   ······@csl.sri.com
Jordan Hubbard: We have a crash bug.  It needs to be fixed. We DO NOT
need to know how to print 3000 spaces in 11 different languages! :-)
Daniel Sobral: I concur. But if anyone wants to do it with loader,
: 3kbl 3000 0 do bl emit loop ; 3kbl will do the trick.
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <lyptirrvub.fsf@cartan.de>
Mario S. Mommer <········@yahoo.com> writes:

> Nils Goesche <······@cartan.de> writes:

> > 	blark[1] = 17;
>              ^^^
> Shouldn't it be 0?

No, it wouldn't be call-by-reference, then :-)

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Sam Zoghaib
Subject: Re: change a global var from a function?
Date: 
Message-ID: <biikch$mtr$1@news-reader3.wanadoo.fr>
Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
August 2003 16:58 in comp.lang.lisp:

>> Shouldn't it be 0?

Yes it should. As I have noted, this is undefined behavior. Unfortunately
(because it hides the problem), it worked on Nils' box. On mine, it does not.

> No, it wouldn't be call-by-reference, then :-)
> 
You still do not acknowledge that C does *not* pass by reference, no matter
what tricks you use. Otherwise, your implementation would not be conforming.

Which does not mean, again, that it's not possible to alter a non global
variable of the calling function in a called function. But that's irrelevant.

Sam
-- 
"People sometimes ask me if it is a sin in the Church of Emacs to use vi.
 Using a free version of vi is not a sin; it's a penance".

        - Richard Stallman
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <lyhe43rskv.fsf@cartan.de>
Sam Zoghaib <·····@zep.homedns.orgSPAM> writes:

> Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
> August 2003 16:58 in comp.lang.lisp:
> 
> >> Shouldn't it be 0?
> 
> Yes it should. As I have noted, this is undefined
> behavior. Unfortunately (because it hides the problem), it worked on
> Nils' box.

Now ain't that some hell of a coincidence?

> On mine, it does not.

Looks like your compiler or OS version isn't supported by us yet ;-)

> > No, it wouldn't be call-by-reference, then :-)

> You still do not acknowledge that C does *not* pass by reference, no
> matter what tricks you use. Otherwise, your implementation would not
> be conforming.

*sigh* I forgot that humor is pretty much a Republican concept not
accessible to all people and hence even a smiley wouldn't help.
Sorry.  Just forget about it.

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Sam Zoghaib
Subject: Re: change a global var from a function?
Date: 
Message-ID: <biipqt$c1m$1@news-reader5.wanadoo.fr>
Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
August 2003 18:08 in comp.lang.lisp:

> *sigh* I forgot that humor is pretty much a Republican concept not
> accessible to all people and hence even a smiley wouldn't help.

I knew there was humor in your post (obviously, from the smiley), but there
was no way I could know whether you did finally understand that there is no
passing by reference in C. The smiley only indicated that there was humor,
not what your final understanding of the subject was.
I am not trying to be a pain in the neck, just trying to help.

Sam
PS: I thought humor was a Democratic concept, not a Republican one :-)
-- 
"Don't be afraid, I'm gonna give you the choice I never had..."

    - Lestat in "Interview with the Vampire" (Ann Rice, 1976)
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <lyad9vrmsn.fsf@cartan.de>
Sam Zoghaib <·····@zep.homedns.orgSPAM> writes:

> Nils Goesche wrote in article <··············@cartan.de> on Wednesday 27
> August 2003 18:08 in comp.lang.lisp:
> 
> > *sigh* I forgot that humor is pretty much a Republican concept not
> > accessible to all people and hence even a smiley wouldn't help.
> 
> I knew there was humor in your post (obviously, from the smiley),
> but there was no way I could know whether you did finally understand
> that there is no passing by reference in C. The smiley only
> indicated that there was humor, not what your final understanding of
> the subject was.  I am not trying to be a pain in the neck, just
> trying to help.

Yeah, my bad.  I thought the silly code I posted was equivalent to one
big smiley :-)

> PS: I thought humor was a Democratic concept, not a Republican one :-)

Looks like you haven't met any Democrats yet :-)

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <87ekz63fe5.fsf@darkstar.cartan>
Mario S. Mommer <········@yahoo.com> writes:

> Nils Goesche <······@cartan.de> writes:
> > 	blark[1] = 17;
>              ^^^
> Shouldn't it be 0?

Sorry, maybe I should explain what's going on here.  I was
assuming that everybody reading comp.lang.lisp is a C wizard but
now I realize that this assumption is probably wrong.  When you
work regularly with very large C or C++ programs, you'll at some
point encounter a certain kind of bug.  Often it will be hard to
reproduce, and whenever you add some printfs to trace it, it will
change its behavior and even more mysterious things happen.
After some days, you'll observe a strange phenomenon: You're
stepping with your debugger through some code, and all of a
sudden some local variables change their values, in a way that
should be absolutely impossible and looks like magic!  Then you
know that you're in trouble: Somebody is corrupting your stack
through a wild pointer!  This is one of the worst things that can
happen to you and often extremely hard to find.  The essence of
the mechanism can be seen in the code I posted.  Here it is
again:

#include <stdio.h>

static void any_function_at_all(int b)
{
	int *blark = &b;

	blark[1] = 17;
}

int main(void)
{
    int b = 42;

    any_function_at_all(b);
    printf("%d\n", b);
    return 0;
}

What happens here is that the local variable b in main is located
somewhere on the stack.  Now, any_function_at_all is called.  The
parameter to this function is placed onto the stack, too, and
blark points to it.  Now, by altering the value of blark, we can
access the other values on the stack, too!  Note that on PCs the
stack typically grows downward, so we increase blark's value.  In
my case, incrementing it once is enough, and now blark is
pointing to the original local variable b in main!  If it doesn't
on your machine, you could try other values than 1, or first
print blark[1], blark[2], ..., blark[20] until you find 42 :-)

This technique will immediately stop working when the compiler
decides not to put b (in main) onto the stack at all.  As C has
call-by-value semantics, it knows that it can as well call
any_function_at_all and printf directly with 42.  That's why you
should try this without optimizations or possibly -O0.  Looking
at the Assemby output with gcc -S might help, too.

What I found particularly hilarious about this example is that I
compiled it with -Wall and didn't even get a warning :-)

Regards,
-- 
Nils G�sche
Mercy to the guilty is cruelty to the innocent.

PGP key ID #xD26EF2A0
From: Erann Gat
Subject: Re: change a global var from a function?
Date: 
Message-ID: <gat-2708031548420001@k-137-79-50-101.jpl.nasa.gov>
In article <··············@darkstar.cartan>, Nils Goesche <···@cartan.de> wrote:

> What happens here is that the local variable b in main is located
> somewhere on the stack.  Now, any_function_at_all is called.  The
> parameter to this function is placed onto the stack, too, and
> blark points to it.  Now, by altering the value of blark, we can
> access the other values on the stack, too!  Note that on PCs the
> stack typically grows downward, so we increase blark's value.  In
> my case, incrementing it once is enough, and now blark is
> pointing to the original local variable b in main!  If it doesn't
> on your machine, you could try other values than 1, or first
> print blark[1], blark[2], ..., blark[20] until you find 42 :-)

Here's a version that ought to work on many different machines:

static void any_function_at_all(int b)
{
  int *blark = &b;

  int i = 2;
  while (1) {
    if (blark[i]==b) {
      blark[i]=99;
      return;
    }
    if (blark[-i]==b) {
      blark[-i]=99;
      return;
    }
    i++;
  }
}

int main(void)
{
  int b = 42;

  any_function_at_all(b);
  printf("%d\n", b);
  return 0;
}

E.
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <871xv63dd9.fsf@darkstar.cartan>
···@jpl.nasa.gov (Erann Gat) writes:

> In article <··············@darkstar.cartan>, Nils Goesche <···@cartan.de> wrote:
> 
> > If it doesn't on your machine, you could try other values
> > than 1, or first print blark[1], blark[2], ..., blark[20]
> > until you find 42 :-)
> 
> Here's a version that ought to work on many different machines:
> 
> static void any_function_at_all(int b)
> {
>   int *blark = &b;
> 
>   int i = 2;
>   while (1) {
>     if (blark[i]==b) {
>       blark[i]=99;
>       return;
>     }
>     if (blark[-i]==b) {
>       blark[-i]=99;
>       return;
>     }
>     i++;
>   }
> }

Good one, although it won't work on /my/ machine unless you start
with `int i = 1;' :-)

Regards,
-- 
Nils G�sche
Mercy to the guilty is cruelty to the innocent.

PGP key ID #xD26EF2A0
From: Hartmann Schaffer
Subject: Re: change a global var from a function?
Date: 
Message-ID: <3f4d3094@news.sentex.net>
In article <··············@cartan.de>,
	Nils Goesche <······@cartan.de> writes:
> ...
> In a powerful language like C, anything is possible:
> 
> If this is boom.c:
> 
> #include <stdio.h>
> 
> static void any_function_at_all(int b)
> {
> 	int *blark = &b;
> 
> 	blark[1] = 17;
> }
> 
> int main(void)
> {
>     int b = 42;
> 
>     any_function_at_all(b);
>     printf("%d\n", b);
>     return 0;
> }
> 
> I get
> 
> ····@pc022[boom]$ gcc -Wall boom.c
> ····@pc022[boom]$ ./a.out 
> 17
> ····@pc022[boom]$ 
> 
> Oops!

which compiler are you using?  and shouldn't that read

static void any_function_at_all(int& b) ?

(in which case it would be C++)

hs

-- 

ceterum censeo SCO esse delendam
From: Nils Goesche
Subject: Re: change a global var from a function?
Date: 
Message-ID: <8765ki3f0r.fsf@darkstar.cartan>
··@heaven.nirvananet (Hartmann Schaffer) writes:

> In article <··············@cartan.de>,
> 	Nils Goesche <······@cartan.de> writes:
> > ...

> > ····@pc022[boom]$ gcc -Wall boom.c
> > ····@pc022[boom]$ ./a.out 
> > 17
> > ····@pc022[boom]$ 
> > 
> > Oops!
> 
> which compiler are you using?  and shouldn't that read
> 
> static void any_function_at_all(int& b) ?
> 
> (in which case it would be C++)

Nope, a C compiler:

······@darkstar:[cartan]$ gcc --version
egcs-2.91.66
······@darkstar:[cartan]$ 

Note that the array index is illegal.

Regards,
-- 
Nils G�sche
Mercy to the guilty is cruelty to the innocent.

PGP key ID #xD26EF2A0
From: Sam Zoghaib
Subject: Re: change a global var from a function?
Date: 
Message-ID: <bigcj0$j7f$1@news-reader2.wanadoo.fr>
Dave wrote in article <························@twister1.libero.it> on Tuesday
26 August 2003 17:46 in comp.lang.lisp:

> void myfunc(char *pstr, char **cstr) {
>     printf(pstr);
>     *cstr = ++pstr;   //direct way to alter (*pstr) argument!!
>     printf(pstr);
> }
> 
> int main(int argc, char *argv[]) {
>     char *str = "Hello, world!\n";
>     myfunc(str, &str);
>     printf(str);
>     return 0;
> }
> 

How is that relevant? You are passing the address of str to the myfunc, so of
course you can modify str. That does not change anything to the fact that C
passes by value arguments to functions; that is, passes *copies* of the
arguments (same value but different address). Passing by reference means that
you pass the actual object. The fact that  you can directly modify the object
or not is a side-effect of the type of passing system.

As an illustration, compile and execute the following code:

--start code--
include <stdio.h>

void myfunc(int j);

int main()
{
    int i = 10;
    printf("%p\n", &i);
    myfunc(i);
    return 0;
}

void myfunc(int j)
{
    printf("%p\n", &j);
}
--end code--

You'll notice that &i != &j

Sam
-- 
"Giving the Linus Torvalds Award to the Free Software Foundation is a bit like
giving the Han Solo Award to the Rebel Alliance"

        - Richard Stallman, August 1999
From: Dave
Subject: Re: change a global var from a function?
Date: 
Message-ID: <tOR2b.267857$Ny5.8243920@twister2.libero.it>
"Sam Zoghaib" <·····@zep.homedns.orgSPAM> ha scritto nel messaggio ·················@news-reader2.wanadoo.fr...
> Dave wrote in article <························@twister1.libero.it> on Tuesday
> 26 August 2003 17:46 in comp.lang.lisp:
> 
> > void myfunc(char *pstr, char **cstr) {
> >     printf(pstr);
> >     *cstr = ++pstr;   //direct way to alter (*pstr) argument!!
> >     printf(pstr);
> > }
> > 
> > int main(int argc, char *argv[]) {
> >     char *str = "Hello, world!\n";
> >     myfunc(str, &str);
> >     printf(str);
> >     return 0;
> > }
> > 
> 
> How is that relevant? You are passing the address of str to the myfunc, so of
> course you can modify str. That does not change anything to the fact that C
> passes by value arguments to functions; that is, passes *copies* of the
> arguments (same value but different address). Passing by reference means that
> you pass the actual object. The fact that  you can directly modify the object
> or not is a side-effect of the type of passing system.
> 
> As an illustration, compile and execute the following code:
> 
> --start code--
> include <stdio.h>
> 
> void myfunc(int j);
> 
> int main()
> {
>     int i = 10;
>     printf("%p\n", &i);
>     myfunc(i);
>     return 0;
> }
> 
> void myfunc(int j)
> {
>     printf("%p\n", &j);
> }
> --end code--
> 
> You'll notice that &i != &j

Yeah! I noticed it, but I can change it!
:-)
From: Michael Tuchman
Subject: Re: change a global var from a function?
Date: 
Message-ID: <zdu2b.2790$Jh2.457@newsread4.news.pas.earthlink.net>
Dave wrote:
> Hi all.
> Is it possible to change a global var. passed as parameter?
> Here an example:
> 
> 
>>(defvar *something* nil)
> 
> 
>>(defun myfunction (globvar)
> 
>       (setq globvar t))

When Lisp sees this, it says globvar is going to be a local variable, 
since globvar matches the name of the parameter to the function.    Part 
of the reason for the convention to put *- -* around global-variable 
names is precisely to avoid this phenomenon.

Lisp seels the variable The result of this function will the the output 
of setq, in this case T.

By contrast, this works as you specified:

(defun set-globvar-to-value (v)
   (setq *globvar* v))
(set-globvar-to-value T)=> T (and *globvar* set to T as side effect)

Lisp, not seeing a parameter matching the name *globvar* , will then 
look to the global environment to resolve the address of *globvar*

Still, there are better ways to accomplish this type of effect. 
Closures are one.   Paul Graham covers the topic in his two books on 
LISP (OnLisp, Ansi Common Lisp).
From: Coby Beck
Subject: Re: change a global var from a function?
Date: 
Message-ID: <bif5ti$hmk$1@otis.netspace.net.au>
"Dave" <·········@iol.it> wrote in message
·····························@twister1.libero.it...

>Hi all.
>Is it possible to change a global var. passed as parameter?
>Here an example:
>
>(defvar *something* nil)
>(defun myfunction (globvar)
>      (setq globvar t))
>
>(myfunction *something*)
>
>(print *something*)
>nil
>
>Why doesn't it change?

Because you have not changed the value you have changed the binding of your
/local/ variable 'globvar'.  You can operate directly on the structure of
your function's parameter if it has some.  For example:
CL-USER 82 > (defun frob (arg)
               (setf (nth 1 arg) 'b))
FROB
CL-USER 83 > (let ((foo (list 1 2 3)))
               (frob foo)
               foo)
(1 B 3)

Thus, as you see, you can destructively modify a function's parameter.  If
you have, however, a special variable (~c global) and you want to effect its
value you would write something like this:

CL-USER 88 > (defvar *foo* (list 1 2 3))
*FOO*
CL-USER 89 > (defun frob2 ()
               (setf (nth 1 *foo*) 'b))
FROB2
CL-USER 90 > *foo*
(1 2 3)
CL-USER 91 > (frob2)
B
CL-USER 92 > *foo*
(1 B 3)
In this case you can also alter the binding:

CL-USER 93 > (defun frob3 ()
               (setf *foo* 6))
FROB3
CL-USER 94 > (frob3)
6
CL-USER 95 > *foo*
6

Though might be would be considered a poor use of special variables
considering what they can really do.  Here is a sample:

CL-USER 96 > (defun frob4 ()
               (let ((*foo* (list 1 2 3)))
                 (print *foo*)
                 (frob2)
                 *foo*))
FROB4
CL-USER 97 > (frob4)
(1 2 3)
(1 B 3)
CL-USER 98 > *foo*
6

As you see, the original value is restore even while any functions called
during the scope of your let form will see the value you rebound it to.

I recommend googling for the long and frequent threads about special vs
local variables and bindings vs values.  Also look for Eran Gat's "Idiot's
Guide to Special Variables"  (don't be put off by the title, the author
claims it refers to him, not the target audience ;)

>In C it's always possible to pass a variable by reference.
>I don't know the way to do it (in Lisp).

Lisp is pass by value but not in the commonly (mis)understood meaning.  To
get the same semantics in a C++ program, you would need a combination of
pointer and reference semantics.  Just think about the fact that there is no
such thing as a copy-constructor in lisp.

-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")