Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 27, 2002 overlapping array copy | ||||
---|---|---|---|---|
| ||||
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 28, 2002 Re: overlapping array copy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos | 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. sorry > 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 Re: overlapping array copy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos | 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 29, 2002 Re: overlapping array copy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "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 29, 2002 Re: overlapping array copy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:ad1qm5$2h26$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); > Who will allocate the memory for the characters in Pavel's version? What happens on buffer overflow in Walter's version? Sandor |
May 29, 2002 Re: overlapping array copy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sandor Hojtsy | "Sandor Hojtsy" <hojtsy@index.hu> wrote in message news:ad2nbk$kv6$1@digitaldaemon.com... > news:ad1qm5$2h26$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); > > > > Who will allocate the memory for the characters in Pavel's version? 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. > 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). |
Copyright © 1999-2021 by the D Language Foundation