Thread overview
overlapping array copy
May 27, 2002
Carlos
May 28, 2002
Carlos
May 28, 2002
Walter
May 29, 2002
Pavel Minayev
May 29, 2002
Sandor Hojtsy
May 29, 2002
Pavel Minayev
May 27, 2002
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
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
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
"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
"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
"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).