www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is the following failing?

reply ryuukk_ <ryuukk.dev gmail.com> writes:
```D
void main()
{
     char[32] id = 0;
     const(char)* str = "hello";

     id = str[0 .. 6];
}

```


it should be a simple memcpy, why DMD complain?

``onlineapp.d(6): Error: mismatched array lengths 32 and 6 for 
assignment `id[] = str[0..6]```

I'm too tired to notice something obvious?
Jan 25
parent reply Hipreme <msnmancini hotmail.com> writes:
On Thursday, 25 January 2024 at 15:20:01 UTC, ryuukk_ wrote:
 ```D
 void main()
 {
     char[32] id = 0;
     const(char)* str = "hello";

     id = str[0 .. 6];
 }

 ```


 it should be a simple memcpy, why DMD complain?

 ``onlineapp.d(6): Error: mismatched array lengths 32 and 6 for 
 assignment `id[] = str[0..6]```

 I'm too tired to notice something obvious?
You need to slice your `id` variable to be the required size. You're trying to assign the complete `id` variable to a slice of size 6. i.e: that should be used instead `id[0..6] = str[0..6]`
Jan 25
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Thursday, 25 January 2024 at 15:22:35 UTC, Hipreme wrote:
 On Thursday, 25 January 2024 at 15:20:01 UTC, ryuukk_ wrote:
 ```D
 void main()
 {
     char[32] id = 0;
     const(char)* str = "hello";

     id = str[0 .. 6];
 }

 ```


 it should be a simple memcpy, why DMD complain?

 ``onlineapp.d(6): Error: mismatched array lengths 32 and 6 for 
 assignment `id[] = str[0..6]```

 I'm too tired to notice something obvious?
You need to slice your `id` variable to be the required size. You're trying to assign the complete `id` variable to a slice of size 6. i.e: that should be used instead `id[0..6] = str[0..6]`
That's dumb, compiler knows that ``id`` has enough room and it should do a simple memcpy, i shouldn't have to do that myself.. i'll stick to calling memcpy.. why stray away from C to do worse
Jan 25
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
```D
void main()
{
     char[32] id = 0;
     id = "hello";
}

```

this works fine, and that is what i expect for the example above..
Jan 25
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Thursday, 25 January 2024 at 15:39:08 UTC, ryuukk_ wrote:
 ```D
 void main()
 {
     char[32] id = 0;
     id = "hello";
 }

 ```

 this works fine, and that is what i expect for the example 
 above..
Raise a bug, I'll fix it.
Jan 25
parent reply Johan <j j.nl> writes:
On Thursday, 25 January 2024 at 16:07:44 UTC, Stefan Koch wrote:
 On Thursday, 25 January 2024 at 15:39:08 UTC, ryuukk_ wrote:
 ```D
 void main()
 {
     char[32] id = 0;
     id = "hello";
 }

 ```

 this works fine, and that is what i expect for the example 
 above..
Raise a bug, I'll fix it.
Hmm. To me, the bug is that string assignment to the array is allowed. Because this also compiles without any compile error: ```D void main() { char[4] id; id = "hello asdad"; } ```
Jan 25
parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Thursday, 25 January 2024 at 17:50:57 UTC, Johan wrote:
 On Thursday, 25 January 2024 at 16:07:44 UTC, Stefan Koch wrote:
 On Thursday, 25 January 2024 at 15:39:08 UTC, ryuukk_ wrote:
 ```D
 void main()
 {
     char[32] id = 0;
     id = "hello";
 }

 ```

 this works fine, and that is what i expect for the example 
 above..
Raise a bug, I'll fix it.
Hmm. To me, the bug is that string assignment to the array is allowed. Because this also compiles without any compile error: ```D void main() { char[4] id; id = "hello asdad"; } ```
I created an issue and included that case https://issues.dlang.org/show_bug.cgi?id=24355
Jan 25