Re: Using "strcpy" to assign value to dynamic char array

2021-11-04 Thread pascal111 via Digitalmars-d-learn
On Thursday, 4 November 2021 at 01:29:57 UTC, jfondren wrote: On Monday, 1 November 2021 at 19:56:13 UTC, pascal111 wrote: But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value

Re: Using "strcpy" to assign value to dynamic char array

2021-11-03 Thread jfondren via Digitalmars-d-learn
On Monday, 1 November 2021 at 19:56:13 UTC, pascal111 wrote: But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value length: { char[] s="xyz".dup; strcpy([0], "Hello World!");

Re: Using "strcpy" to assign value to dynamic char array

2021-11-03 Thread Imperatorn via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 10:38:45 UTC, pascal111 wrote: On Wednesday, 3 November 2021 at 02:38:56 UTC, Steven Schveighoffer wrote: On 11/1/21 9:03 PM, pascal111 wrote: [...] [...] ... [...] Please please, do NOT study this code. It is bad all around. Ali should know better ;)

Re: Using "strcpy" to assign value to dynamic char array

2021-11-03 Thread pascal111 via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 02:38:56 UTC, Steven Schveighoffer wrote: On 11/1/21 9:03 PM, pascal111 wrote: On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote: Joking aside ... This function seems smart and flexible and higher than my current level, I'll study it. Please

Re: Using "strcpy" to assign value to dynamic char array

2021-11-03 Thread Ali Çehreli via Digitalmars-d-learn
On 11/2/21 9:06 PM, tsbockman wrote: UFCS works for setters, too: Oh yeah! Pretty cool. :) Ali

Re: Using "strcpy" to assign value to dynamic char array

2021-11-02 Thread tsbockman via Digitalmars-d-learn
On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote: Joking aside, I liked the nested struct and its opAssign to mimic internal `arr.length = 42` syntax. (I know it involves a potentially expensive delegate but still...) The nested struct is not needed. UFCS works for setters, too:

Re: Using "strcpy" to assign value to dynamic char array

2021-11-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/1/21 9:03 PM, pascal111 wrote: On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote: Joking aside ... This function seems smart and flexible and higher than my current level, I'll study it. Please please, do NOT study this code. It is bad all around. Ali should know

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 November 2021 at 21:37:59 UTC, Ali Çehreli wrote: On 11/1/21 2:28 PM, pascal111 wrote: > This can serve the style I want. I am feeling funny right now and showing incorrect code. It's impossible to fit "Hello World!" in "xyz". As Steve said, don't do that. :) > It uses OOP

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote: On 11/1/21 2:01 PM, Ali Çehreli wrote: > The program is as incorrect as its C equivalent would be. ;) I wrote a cool function to make it easy to disregard memory safety: import std.stdio; auto assumedLength(S)(ref S slice) {

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread Ali Çehreli via Digitalmars-d-learn
On 11/1/21 2:28 PM, pascal111 wrote: > This can serve the style I want. I am feeling funny right now and showing incorrect code. It's impossible to fit "Hello World!" in "xyz". As Steve said, don't do that. :) > It uses OOP style like C++ by putting a > pointer as a property, D's slices are

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread Ali Çehreli via Digitalmars-d-learn
On 11/1/21 2:01 PM, Ali Çehreli wrote: > The program is as incorrect as its C equivalent would be. ;) I wrote a cool function to make it easy to disregard memory safety: import std.stdio; auto assumedLength(S)(ref S slice) { struct LengthSetter { void opAssign(size_t length) { //

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 November 2021 at 21:01:31 UTC, Ali Çehreli wrote: On 11/1/21 1:49 PM, pascal111 wrote: > Yes, I'm practicing doing things in low level style like standard C. All you needed extra was to let the slice know about the new length: import std.stdio; import core.stdc.string; void

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread Ali Çehreli via Digitalmars-d-learn
On 11/1/21 1:49 PM, pascal111 wrote: > Yes, I'm practicing doing things in low level style like standard C. All you needed extra was to let the slice know about the new length: import std.stdio; import core.stdc.string; void main() { char[] s="xyz".dup; strcpy([0], "Hello World!"); s =

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 November 2021 at 20:15:14 UTC, Steven Schveighoffer wrote: On 11/1/21 3:56 PM, pascal111 wrote: But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value length: {

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/1/21 3:56 PM, pascal111 wrote: But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value length: { char[] s="xyz".dup; strcpy([0], "Hello World!"); writeln(s); } Result:

Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread pascal111 via Digitalmars-d-learn
I know that I can use the next syntax to assign new value to char dynamic array, and the new value isn't in same length of the current value of the array: { char[] s="xyz".dup; s="Hello World!".dup; writeln(s); } Result: Hello World! = But what if I want to use