www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Creating a pointer array

reply data pulverizer <data.pulverizer gmail.com> writes:
Hi all,

How do you create an array of pointers in D? I tried something 
like

```
double* []y;
```

Or

```
(double*) []y;
```

But I get the error:

```
Error: only one index allowed to index double[]
```

Thanks in advance.
Aug 19 2020
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 19 August 2020 at 13:03:54 UTC, data pulverizer 
wrote:
 How do you create an array of pointers in D? I tried something 
 like

 ```
 double* []y;
 ```
I'd write it double*[] y; but yeah that's it.
 Error: only one index allowed to index double[]
That must be at the usage point where you prolly just need parens or something.
Aug 19 2020
parent reply data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 19 August 2020 at 13:08:37 UTC, Adam D. Ruppe wrote:
 On Wednesday, 19 August 2020 at 13:03:54 UTC, data pulverizer 
 wrote:
 How do you create an array of pointers in D? I tried something 
 like

 ```
 double* []y;
 ```
I'd write it double*[] y; but yeah that's it.
 Error: only one index allowed to index double[]
That must be at the usage point where you prolly just need parens or something.
Argh, Sorry! The error was from the line before! False alarm. I was wandering why something so obvious wasn't working. Thanks anyway.
Aug 19 2020
parent data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 19 August 2020 at 13:12:21 UTC, data pulverizer 
wrote:
 On Wednesday, 19 August 2020 at 13:08:37 UTC, Adam D. Ruppe 
 wrote:
 On Wednesday, 19 August 2020 at 13:03:54 UTC, data pulverizer 
 wrote:
 How do you create an array of pointers in D? I tried 
 something like

 ```
 double* []y;
 ```
I'd write it double*[] y; but yeah that's it.
 Error: only one index allowed to index double[]
That must be at the usage point where you prolly just need parens or something.
Argh, Sorry! The error was from the line before! False alarm. I was wandering why something so obvious wasn't working. Thanks anyway.
For the record using: ``` (double*)[] data; ``` gives an error ``` Error: found * when expecting . following double Error: found ) when expecting identifier following double. Error: found data when expecting ) ``` the other version was fine.
Aug 19 2020
prev sibling parent reply bachmeier <no spam.net> writes:
On Wednesday, 19 August 2020 at 13:03:54 UTC, data pulverizer 
wrote:
 Hi all,

 How do you create an array of pointers in D? I tried something 
 like

 ```
 double* []y;
 ```

 Or

 ```
 (double*) []y;
 ```

 But I get the error:

 ```
 Error: only one index allowed to index double[]
 ```

 Thanks in advance.
Maybe I'm the only one, but I think double*[] is hideous, and I'd sure hate for someone not used to D to see it. Alias is your friend. I think this is much nicer: void main() { alias double* DoublePtr; DoublePtr[] arr; auto v = [1.0, 2.0, 3.0]; auto w = [4.0, 5.0, 6.0]; arr ~= [v.ptr, w.ptr]; writeln(arr); }
Aug 19 2020
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Aug 19, 2020 at 08:09:31PM +0000, bachmeier via Digitalmars-d-learn
wrote:
[...]
 Maybe I'm the only one, but I think double*[] is hideous, and I'd sure
 hate for someone not used to D to see it.
IMO, double*[] is absolutely logical. It's a natural consequence of type syntax.
 Alias is your friend. I think this is much nicer:
 
 void main() {
     alias double* DoublePtr;
     DoublePtr[] arr;
[...] IMO, this is far too verbose. If I had 10 arrays of 10 different types of pointers, I wouldn't want to write 10 aliases just for that. Plus, hiding things behind an alias means someone who reads your code has to look up the alias definition to figure out what it means, whereas if they see double*[] the exact meaning is immediately obvious. I'd only use an alias for inordinately-long type names, like function pointers or delegates with lots of parameters, or verbose type names like const(int[])[string]. (And even in the latter case, only when the name occurs frequently.) But as they say, YMMV. :-) T -- "A man's wife has more power over him than the state has." -- Ralph Emerson
Aug 19 2020
prev sibling parent reply data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 19 August 2020 at 20:09:31 UTC, bachmeier wrote:
 On Wednesday, 19 August 2020 at 13:03:54 UTC, data pulverizer 
 wrote:
 Maybe I'm the only one, but I think double*[] is hideous, and 
 I'd sure hate for someone not used to D to see it. Alias is 
 your friend. I think this is much nicer:

 void main() {
     alias double* DoublePtr;
     DoublePtr[] arr;
     auto v = [1.0, 2.0, 3.0];
     auto w = [4.0, 5.0, 6.0];
     arr ~= [v.ptr, w.ptr];
     writeln(arr);
 }
I know what you mean. I'm using something like this: ``` alias P(T) = T*; P!(double)[] arr; ``` alias is one of those awesome chameleons in D. The template equivalent is ``` template P(T) = T*; ``` In this case I wonder if there is any difference as far as the compiler is concerned?
Aug 19 2020
parent reply data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 19 August 2020 at 21:10:00 UTC, data pulverizer 
wrote:
 alias is one of those awesome chameleons in D. The template 
 equivalent is

 ```
 template P(T) = T*;
 ```
Correction ... ``` template P(T){ alias P = T*; } ```
Aug 19 2020
parent data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 19 August 2020 at 22:21:21 UTC, data pulverizer 
wrote:
 On Wednesday, 19 August 2020 at 21:10:00 UTC, data pulverizer 
 wrote:
 alias is one of those awesome chameleons in D. The template 
 equivalent is

 ```
 template P(T) = T*;
 ```
Correction ... ``` template P(T){ alias P = T*; } ```
... just realised, ``` alias P(T) = T*; ``` Is just shorthand for ... ``` template P(T) { alias P = T*; } ``` Just as functions, classes, structs, and enums have template shorthands. Lol!
Aug 22 2020