Re: segfault w/ block, but not file scope



Chris Torek wrote:

[..]

Mr Dionne appears to believe that C's arrays are an exception, and
are passed by reference.  While C's arrays *are* exceptional, this
is not where the exception occurs.  C remains pass-by-value.  The
gimmick is that the "value" of an array is a pointer to the array's
first element.  In:

    void f(void) {
        char buf[100];
        char *p;

        p = buf;

we attempt to copy the "value" of buf -- an array -- to the pointer
variable p.  The "value" of the array is a pointer to the array's
first element, so this sets p to point to &buf[0].  Likewise, if we
go on to call a function g() and pass the "value" of buf:

        g(buf);
        ...
    }

then g() receives, as its value, a pointer to the array's first
element -- a pointer to buf[0].  Within g(), as for any simulation
of by-reference in C, we have to use the unary "*" operator in
order to write to buf[0]:

    void g(char * ptr) {
        * ptr = 42;
        ...
    }

and if we fail to prefix "ptr" with the unary "*" operator when
assigning, we will "re-bind" it so that it no longer points to
"buf" at all:

        ptr = "oops";

Now *ptr is no longer 42 (in any current character set anyway):
ptr now points to the first element of an anonymous (unnamed) array
of 5 "char"s that are set to {'o','o','p','s','\0'} and must not
be changed (they may or may not actually be read-only, but the
effect of attempting to change them is undefined).


Look who is being humorous. Sir, in you example, "buf" will never change is memory value, even *if* you pass it by reference, "& buf". You might cause a program error, but buf will forever point to the same memory address.



[..]

I think we all can agree the pascal and c++ support pass by reference by different syntax. I assert the c uses yet another syntax to implement pass by reference, nothing more or less.
.




Relevant Pages

  • Re: assert
    ... reference class instances the array can only contain pointers to the ... classes that are internally default public): reference classes and value ... via just its pointer. ... static void Assert(bool condition) ...
    (microsoft.public.dotnet.languages.vc)
  • Re: "Must instantiate controlled types at library level." Why?
    ... > The array bounds become unknown. ... > just the size of an pointer. ... It also show that operators attached to a reference type act ... > the compiler will stop creating copy constructors and warn you if you ...
    (comp.lang.ada)
  • Re: assert
    ... value class valClass ... create the array POINTER. ... by passing JUST the pointer to rc it has virtually passed ... classes that are internally default public): reference classes and value ...
    (microsoft.public.dotnet.languages.vc)
  • Re: pass an array throuh a function
    ... >Doesn't this just demonstrate a way of using a reference ... >to the pointer. ... You have still not passed the actual array ... If you call show2 passing an actual, honest-to-goodness float *, however ...
    (alt.comp.lang.learn.c-cpp)
  • Re: assert
    ... value class valClass ... create the array POINTER. ... by passing JUST the pointer to rc it has virtually passed all ... classes that are internally default public): reference classes and value ...
    (microsoft.public.dotnet.languages.vc)