* Re: [9fans] thread
@ 2001-07-06 4:15 arisawa
2001-07-06 14:41 ` Dan Cross
0 siblings, 1 reply; 12+ messages in thread
From: arisawa @ 2001-07-06 4:15 UTC (permalink / raw)
To: 9fans
Thank you, Dan,
You say:
>No, &m in this context is perfectly legal C. ``&'' in C says
>to take the address of the referenced object (m)
But, how can I understand the following result?
#include <libc.h>
main(){
int a[1];
a[0] = 10;
print("%d\n",*a);
print("%d\n",a);
print("%d\n",&a);
print("%d\n",*&a);
print("%d\n",*(&a));
print("%d\n",*(a));
}
The output is:
10
2147479448
2147479448
2147479448
2147479448
10
Kenji Arisawa
E-mail: arisawa@aichi-u.ac.jp
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
2001-07-06 4:15 [9fans] thread arisawa
@ 2001-07-06 14:41 ` Dan Cross
2001-07-06 18:24 ` Boyd Roberts
0 siblings, 1 reply; 12+ messages in thread
From: Dan Cross @ 2001-07-06 14:41 UTC (permalink / raw)
To: 9fans
In article <20010706042502.AE409199D7@mail.cse.psu.edu> you write:
>Thank you, Dan,
Thanks, Kenji, though I think that Dennis' note was better. :-)
#include <u.h>
>#include <libc.h>
>main(){
> int a[1];
> a[0] = 10;
> print("%d\n",*a);
> print("%d\n",a);
> print("%d\n",&a);
> print("%d\n",*&a);
> print("%d\n",*(&a));
These are the two that confused me, since they output:
>2147479448
>2147479448
However, Dennis clarified how it worked when he pointed out the
type differences between a and &a (the former is int *, the latter
int (*)[1]). I believe that in the case of *&a, you end up with
a variable of type a[1], which decays into int *. Restated another
way, it's as if the * and & cancel each other out, leaving you
an unadorned a, which turns into int *.
At least, I believe that's how it works; I'd love to be corrected
if I'm wrong. Since this isn't comp.lang.c, I can actually look
forward to such a thing. ;-p
- Dan C.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
2001-07-06 14:41 ` Dan Cross
@ 2001-07-06 18:24 ` Boyd Roberts
0 siblings, 0 replies; 12+ messages in thread
From: Boyd Roberts @ 2001-07-06 18:24 UTC (permalink / raw)
To: 9fans
> However, Dennis clarified how it ...
he did, but do not forget that he said that printf
is varadic.
do not forget either that using %d with pointers (to int
or anything thing else) is fraught with peril.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
2001-07-09 11:46 ` Digby Tarvin
@ 2001-07-09 17:03 ` Dan Cross
0 siblings, 0 replies; 12+ messages in thread
From: Dan Cross @ 2001-07-09 17:03 UTC (permalink / raw)
To: 9fans
In article <200107091146.MAA27082@cthulhu.dircon.co.uk> you write:
>I think you misunderstand. I was referring to the application of '&' to
>a variable of array type, not to a type. Applied to an array type, it
^^ a variable of
>produces a change of type but no change in value.
Errm, isn't this implementation dependent?
>An unusual case
>where the '&' operator could be replaced by a cast with no change
>in semantics. I would say that was special treatment, (resulting
>from the special treatments of array names) albeit in accordance
>with the C standard.
But the semantics are different, as Dennis pointed out in his post.
- Dan C.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
2001-07-09 8:33 ` Douglas A. Gwyn
2001-07-09 11:46 ` Digby Tarvin
@ 2001-07-09 11:49 ` Boyd Roberts
1 sibling, 0 replies; 12+ messages in thread
From: Boyd Roberts @ 2001-07-09 11:49 UTC (permalink / raw)
To: 9fans
From: "Douglas A. Gwyn" <DAGwyn@null.net>
> Digby Tarvin wrote:
> > I was aware that the array type was special in that the application of
> > the '&' operator to it resulted in a change of type but not of value.
>
> ? One cannot (meaningfully) apply & to a type, only to a certain kind
> of expression. When that expression consists of an identifier that
> has been declared as an object having array type, then in fact
> &that_identifier is *not* treated specially, according to the C
> standard, but rather follows the generic rules for the & operator.
the one time illegal &array construct falls out of the history of C.
the reason being this is that an array identifier was not an lvalue,
but an lvalue-expression.
read what dennis has to say at:
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html
ps. i know doug knows this.
pps. there's http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf too.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
2001-07-09 8:33 ` Douglas A. Gwyn
@ 2001-07-09 11:46 ` Digby Tarvin
2001-07-09 17:03 ` Dan Cross
2001-07-09 11:49 ` Boyd Roberts
1 sibling, 1 reply; 12+ messages in thread
From: Digby Tarvin @ 2001-07-09 11:46 UTC (permalink / raw)
To: 9fans
I think you misunderstand. I was referring to the application of '&' to
a variable of array type, not to a type. Applied to an array type, it
produces a change of type but no change in value. An unusual case
where the '&' operator could be replaced by a cast with no change
in semantics. I would say that was special treatment, (resulting
from the special treatments of array names) albeit in accordance
with the C standard.
Regards,
DigbyT
Douglas A. Gwyn:
> Digby Tarvin wrote:
> > I was aware that the array type was special in that the application of
> > the '&' operator to it resulted in a change of type but not of value.
>
> ? One cannot (meaningfully) apply & to a type, only to a certain kind
> of expression. When that expression consists of an identifier that
> has been declared as an object having array type, then in fact
> &that_identifier is *not* treated specially, according to the C
> standard, but rather follows the generic rules for the & operator.
> What is special is the special rule that the unadorned identifier
> decays, in most contexts, into a pointer to the first element of the
> array designated by that identifier; this is closely connected with
> arrays not being first-class objects in C. It is often convenient,
> but is not natural.
>
--
Digby R. S. Tarvin digbyt@acm.org
http://www.cthulhu.dircon.co.uk
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
2001-07-06 16:54 ` Digby Tarvin
@ 2001-07-09 8:33 ` Douglas A. Gwyn
2001-07-09 11:46 ` Digby Tarvin
2001-07-09 11:49 ` Boyd Roberts
0 siblings, 2 replies; 12+ messages in thread
From: Douglas A. Gwyn @ 2001-07-09 8:33 UTC (permalink / raw)
To: 9fans
Digby Tarvin wrote:
> I was aware that the array type was special in that the application of
> the '&' operator to it resulted in a change of type but not of value.
? One cannot (meaningfully) apply & to a type, only to a certain kind
of expression. When that expression consists of an identifier that
has been declared as an object having array type, then in fact
&that_identifier is *not* treated specially, according to the C
standard, but rather follows the generic rules for the & operator.
What is special is the special rule that the unadorned identifier
decays, in most contexts, into a pointer to the first element of the
array designated by that identifier; this is closely connected with
arrays not being first-class objects in C. It is often convenient,
but is not natural.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
2001-07-06 5:55 dmr
@ 2001-07-06 16:54 ` Digby Tarvin
2001-07-09 8:33 ` Douglas A. Gwyn
0 siblings, 1 reply; 12+ messages in thread
From: Digby Tarvin @ 2001-07-06 16:54 UTC (permalink / raw)
To: 9fans
Hi,
That is a fascinating little subtlety which I hadn't appreciated
before.
I was aware that the array type was special in that the application of
the '&' operator to it resulted in a change of type but not of value.
But if I understand correctly, there is a converse situation with
the type '(*)[n]' (ie address of an array) where the application
of the unary '*' operator also produces a change of type but not value.
Or said another way, the address of an array of ints is a different type
to an array of ints, but for any given array the two values are identical:
main()
{
int a[1];
int (*la)[1];
a[0] = 10;
la = &a;
printf(" a = %x\n", a); /* array of ints */
printf(" &a = %x\n", &a); /* address of array of ints */
printf(" la = %x\n", la); /* address of array of ints */
printf("*la = %x\n", *la); /* array of ints */
}
Which produces (gcc on BSD - sorry, Plan9 is not at hand)
a = efbfd91c
&a = efbfd91c
la = efbfd91c
*la = efbfd91c
Subtle, but at least it is symmetric.
Regards,
DigbyT
dmr@plan9.bell-labs.com:
> So far as I can determine, the Plan 9 C compiler is
> conformant here.
>
> If you have, say, an
>
> int A[10];
>
> then just A is the address of A and has type int *.
> &A is a pointer to this array of 10 integers; it will
> have the same value, as a pointer, but a different type:
>
> int (*)[10]
>
[snip]
>
> Dennis
--
Digby R. S. Tarvin digbyt@acm.org
http://www.cthulhu.dircon.co.uk
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
@ 2001-07-06 5:55 dmr
2001-07-06 16:54 ` Digby Tarvin
0 siblings, 1 reply; 12+ messages in thread
From: dmr @ 2001-07-06 5:55 UTC (permalink / raw)
To: 9fans
So far as I can determine, the Plan 9 C compiler is
conformant here.
If you have, say, an
int A[10];
then just A is the address of A and has type int *.
&A is a pointer to this array of 10 integers; it will
have the same value, as a pointer, but a different type:
int (*)[10]
The reason why the initialization in the thread man page,
(and presumably the code) works is that the thing being
assigned to is void *, and any object pointer will fit; the
coercion just happens.
The & in &m is redundant, probably misleading, but ultimately
harmless. Kenji's example with print likewise has the
same character, since print is a variadic function, and all
his second arguments fall under the ... in its declaration,
and so aren't type-checked.
Incidentally, there is no C-language guarantee that the
actual bit pattern produced by A and &A will be identical,
though in practice they usually will be for most compilers,
yet they still have different types.
For a case in which the type-checking is more evident, without
the laxness of void * or ..., try compiling
#include <u.h>
#include <libc.h>
void t1(int *ip);
void t2(int (*iap)[10]);
void
main(void)
{
int ia[10];
t1(ia);
t1(&ia);
t2(ia);
t2(&ia);
}
You will get errors on lines 13 [ t1(&ia) ] and
14 [ t2(ia) ].
Dennis
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
2001-07-06 0:50 arisawa
@ 2001-07-06 2:05 ` Dan Cross
0 siblings, 0 replies; 12+ messages in thread
From: Dan Cross @ 2001-07-06 2:05 UTC (permalink / raw)
To: 9fans
In article <20010706010009.D94AF199C0@mail.cse.psu.edu> you write:
>Hello 9fans,
Hey Kenji,
>Manual of thread shows the example codes such as:
> char m[48];
> ...
> {nil, &m, CHANRCV},
>I wonder that &m is a typo.
No, &m in this context is perfectly legal C. ``&'' in C says
to take the address of the referenced object (m), where m refers
to that 48 char array. However, ``m'' by itself will also decay
into a pointer to the array, so the two uses are equivalent in
this context. At least, that's the gist of it; Doug Gywn would
be able to describe it more precisely than I have.
As Rob says, though, the & is irrelevant, and I've seen it be
confusing in the past; particularly for junior programmers. I
recommend writing C code which references arrays without the &.
- Dan C.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [9fans] thread
@ 2001-07-06 1:02 rob pike
0 siblings, 0 replies; 12+ messages in thread
From: rob pike @ 2001-07-06 1:02 UTC (permalink / raw)
To: 9fans
[-- Attachment #1: Type: text/plain, Size: 32 bytes --]
Unnecessary, anyway.
-rob
[-- Attachment #2: Type: message/rfc822, Size: 1370 bytes --]
From: arisawa@ar.aichi-u.ac.jp
To: 9fans@cse.psu.edu
Subject: [9fans] thread
Date: Fri, 6 Jul 2001 09:50:20 +0900
Message-ID: <20010706010009.D94AF199C0@mail.cse.psu.edu>
Hello 9fans,
Manual of thread shows the example codes such as:
char m[48];
...
{nil, &m, CHANRCV},
I wonder that &m is a typo.
Thanks,
Kenji Arisawa
E-mail: arisawa@aichi-u.ac.jp
^ permalink raw reply [flat|nested] 12+ messages in thread
* [9fans] thread
@ 2001-07-06 0:50 arisawa
2001-07-06 2:05 ` Dan Cross
0 siblings, 1 reply; 12+ messages in thread
From: arisawa @ 2001-07-06 0:50 UTC (permalink / raw)
To: 9fans
Hello 9fans,
Manual of thread shows the example codes such as:
char m[48];
...
{nil, &m, CHANRCV},
I wonder that &m is a typo.
Thanks,
Kenji Arisawa
E-mail: arisawa@aichi-u.ac.jp
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2001-07-09 17:03 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-06 4:15 [9fans] thread arisawa
2001-07-06 14:41 ` Dan Cross
2001-07-06 18:24 ` Boyd Roberts
-- strict thread matches above, loose matches on Subject: below --
2001-07-06 5:55 dmr
2001-07-06 16:54 ` Digby Tarvin
2001-07-09 8:33 ` Douglas A. Gwyn
2001-07-09 11:46 ` Digby Tarvin
2001-07-09 17:03 ` Dan Cross
2001-07-09 11:49 ` Boyd Roberts
2001-07-06 1:02 rob pike
2001-07-06 0:50 arisawa
2001-07-06 2:05 ` Dan Cross
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).