D - overlapping array copy
- Carlos (18/18) May 26 2002 If strings have to be read like this:
- Carlos (2/20) May 27 2002
- Walter (5/18) May 28 2002 The trouble here is that scanf() expects a pointer to a string, not a
- Pavel Minayev (5/8) May 28 2002 Or:
- Sandor Hojtsy (5/12) May 29 2002 Who will allocate the memory for the characters in Pavel's version?
- Pavel Minayev (8/19) May 29 2002 Stream::scanf() will. When it sees "%.*s", it automatically allocates
If strings have to be read like this: char [] str; char [80] tmp; scanf("%s",tmp); str=tmp[0..strlen(tmp)]; why doesn't it work?: void inputString(inout char[] a) { char [80] w; fflush(stdin); printf("%.*s",mensaje); scanf("%s",w); a=w[0..strlen(w)]; } It can be compiled, and the program actually runs, but AFTER it reads the string, this message is shown: Error: overlapping array copy and doesn't say anything else.
May 26 2002
Just realized of something:If strings have to be read like this: char [] str; char [80] tmp; scanf("%s",tmp); str=tmp[0..strlen(tmp)]; why doesn't it work?: void inputString(inout char[] a) { char [80] w; fflush(stdin); printf("%.*s",mensaje);this line (printf...) doesn't belong to this function. sorryscanf("%s",w); a=w[0..strlen(w)]; } It can be compiled, and the program actually runs, but AFTER it reads the string, this message is shown: Error: overlapping array copy and doesn't say anything else.
May 27 2002
The trouble here is that scanf() expects a pointer to a string, not a dynamic array. To correct the problem, use: scanf("%s", (char *)w); "Carlos" <carlos8294 msn.com> wrote in message news:acs4dc$1sff$1 digitaldaemon.com...why doesn't it work?: void inputString(inout char[] a) { char [80] w; fflush(stdin); printf("%.*s",mensaje); scanf("%s",w); a=w[0..strlen(w)]; } It can be compiled, and the program actually runs, but AFTER it reads the string, this message is shown: Error: overlapping array copy and doesn't say anything else.
May 28 2002
"Walter" <walter digitalmars.com> wrote in message news:ad0vi8$g7s$1 digitaldaemon.com...The trouble here is that scanf() expects a pointer to a string, not a dynamic array. To correct the problem, use: scanf("%s", (char *)w);Or: char[] w; stdin.scanf("%.*s", w);
May 28 2002
"Pavel Minayev" <evilone omen.ru> wrote in message news:ad1qm5$2h26$1 digitaldaemon.com...Who will allocate the memory for the characters in Pavel's version? What happens on buffer overflow in Walter's version? SandorThe trouble here is that scanf() expects a pointer to a string, not a dynamic array. To correct the problem, use: scanf("%s", (char *)w);Or: char[] w; stdin.scanf("%.*s", w);
May 29 2002
"Sandor Hojtsy" <hojtsy index.hu> wrote in message news:ad2nbk$kv6$1 digitaldaemon.com...news:ad1qm5$2h26$1 digitaldaemon.com...Stream::scanf() will. When it sees "%.*s", it automatically allocates the amount of bytes required to store the string read. You don't have to worry about it at all.Who will allocate the memory for the characters in Pavel's version?The trouble here is that scanf() expects a pointer to a string, not a dynamic array. To correct the problem, use: scanf("%s", (char *)w);Or: char[] w; stdin.scanf("%.*s", w);What happens on buffer overflow in Walter's version?GPF, if you are lucky. Otherwise, some garbage will be written into the memory (which could be the placeholder for another array you've declared).
May 29 2002