Re: segfault w/ block, but not file scope
- From: Joseph Dionne <jdionne@xxxxxxxxxxx>
- Date: Sun, 08 Jan 2006 05:10:44 GMT
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.
.
- Follow-Ups:
- Re: segfault w/ block, but not file scope
- From: Chris Torek
- Re: segfault w/ block, but not file scope
- From: Richard Heathfield
- Re: segfault w/ block, but not file scope
- References:
- segfault w/ block, but not file scope
- From: Dieter
- Re: segfault w/ block, but not file scope
- From: Richard Heathfield
- Re: segfault w/ block, but not file scope
- From: Keith Thompson
- Re: segfault w/ block, but not file scope
- From: Joseph Dionne
- Re: segfault w/ block, but not file scope
- From: Chris Torek
- segfault w/ block, but not file scope
- Prev by Date: Re: Initial values of File scoped and Block level variables
- Next by Date: Re: segfault w/ block, but not file scope
- Previous by thread: Re: segfault w/ block, but not file scope
- Next by thread: Re: segfault w/ block, but not file scope
- Index(es):
Relevant Pages
|