www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [Confusion] more on AA's

reply Manfred Nowak <svv1999 hotmail.com> writes:
Consider

<code>
void main(){
 int [ char[]] aa;
 printf("%d\n", aa["x"]);
 printf("%d\n", aa.length);

 char[]* p=cast(char[]*)("x" in aa);
 p != null && printf("%.*s\n", *p);
}
</code>

<output>
0
1
(null)
</output>

Questions:

1) According to the second line of the output the length of the AA is 
increased by the access to the value of "x". Where is documented that the 
access to the _value_ of a key inserts the key into the AA and why is 
this a useful side effect?

2)
<specs>
The InExpression yields a pointer to the value if the key is in the 
associative array, or null if not
</specs>

According to the first line of the output the value of the key `"x"' is  
"0" and according to the retrieval preceding the `printf' the value of 
`p' is not `null'. According to the specs the third line should then hold 
a "0" instead of a "(null)". So what is wrong here: me, the first or the 
third line of this output?

Hint: multiple choice :-)

-manfred
Jan 25 2005
parent reply "Carlos Santander B." <csantander619 gmail.com> writes:
Manfred Nowak wrote:
 Consider
 
 <code>
 void main(){
  int [ char[]] aa;
  printf("%d\n", aa["x"]);
  printf("%d\n", aa.length);
 
  char[]* p=cast(char[]*)("x" in aa);
  p != null && printf("%.*s\n", *p);
 }
 </code>
 
 <output>
 0
 1
 (null)
 </output>
 
 Questions:
 
 1) According to the second line of the output the length of the AA is 
 increased by the access to the value of "x". Where is documented that the 
 access to the _value_ of a key inserts the key into the AA and why is 
 this a useful side effect?
 

I don't think it's documented, but it's known behavior. Some don't like it, some don't mind. I'm in the second group.
 2)
 <specs>
 The InExpression yields a pointer to the value if the key is in the 
 associative array, or null if not
 </specs>
 
 According to the first line of the output the value of the key `"x"' is  
 "0" and according to the retrieval preceding the `printf' the value of 
 `p' is not `null'. According to the specs the third line should then hold 
 a "0" instead of a "(null)". So what is wrong here: me, the first or the 
 third line of this output?

 Hint: multiple choice :-)

I modified your code a bit to answer this: //------------------------------------ void main(){ int [ char[]] aa; printf("%d\n", aa["x"]); printf("%d\n", aa.length); int * xxx="x" in aa; printf("%x, %d\n",xxx,*xxx); char[]* p=cast(char[]*)xxx; // char[]* p=cast(char[]*)("x" in aa); p != null && printf("{%.*s}\n", *p); } //------------------------------------ The output I get is (dmd 0.111 for Win): //------------------------------------ 0 1 900fd4, 0 {} //------------------------------------ So, I'd say you're wrong. "p" is not null: it's pointing to where "xxx" is pointing, which is the address of aa["x"] (900fd4 in my case). Now, printf("%.*s") is expecting a length and a pointer, but since "p" is pointing to a 0, that's the length printf is reading, so it doesn't print anything. I hope I got all of this right.
 
 -manfred

_______________________ Carlos Santander Bernal
Jan 25 2005
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
"Carlos Santander B." wrote: 

[...]
 I don't think it's documented, but it's known behavior. Some don't
 like it, some don't mind. I'm in the second group.

Thanks for the hint. I found digitalmars.D/13789 on this issue. I have been away for a couple of month :-( [...]
      int * xxx="x" in aa;
      printf("%x, %d\n",xxx,*xxx);

Ahh, now I see my error. Because of the former post, where I mentioned that one is forced to see the key in the expression `b["hello"]' and not the value, I blindly overread, that a pointer to the value is returned and not to the key. This blindness reached that far, that I casted the unexpected `int*' to a `char[]*', even not noticing this, when writing the post. [...]
 so it doesn't print anything.

If it hadn't print anything, i.e. "", my error would have gone unnoticed and unpublished, but it printed "(none)" and I still do not understand why. -manfred
Jan 25 2005
next sibling parent "Walter" <newshound digitalmars.com> writes:
"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:ct6q18$1s4$1 digitaldaemon.com...
 If it hadn't print anything, i.e. "", my error would have gone unnoticed
 and unpublished, but it printed "(none)" and I still do not understand
 why.

printf will print (null) on seeing a null pointer, despite the length for .*s being 0. writef will print no characters.
Jan 26 2005
prev sibling parent "Carlos Santander B." <csantander619 gmail.com> writes:
Manfred Nowak wrote:
 "Carlos Santander B." wrote: 
so it doesn't print anything.

If it hadn't print anything, i.e. "", my error would have gone unnoticed and unpublished, but it printed "(none)" and I still do not understand why. -manfred

That's just what I got. I don't know either why it gave you something else. _______________________ Carlos Santander Bernal
Jan 31 2005