Jump to page: 1 2
Thread overview
scanf, char*, access violation
Jan 28, 2003
Andrew Edwards
Jan 28, 2003
Andrew Edwards
Jan 29, 2003
Burton Radons
Jan 29, 2003
Burton Radons
January 28, 2003
This compiles correctly:

import c.stdio;
int main(char[][] args) {
        char[] a;
        char[80] b;
        printf('hey: ');
        ubyte n=scanf('%s',b);
        printf('.');
        a=b[0..n-1];
        printf('%.*s'\n,a);
        return 0;
}

But this is the output I get when I run it:

hey: hey
Error: Access Violation

It doesn't pass the scanf(). Why?

-------------------------
Carlos Santander
http://carlos3.netfirms.com/


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 2003-01-27


January 28, 2003
"Carlos Santander B." <carlos8294@msn.com> wrote in message news:b16l6l$t8p$1@digitaldaemon.com...
> It doesn't pass the scanf(). Why?

try this:

import c.stdio;
int main(char[][] args) {
        char[] a;
        char[80] b;
        printf('hey: ');
        ubyte n=scanf('%s',&b);
        printf('.');
        a = b;
        printf("%.*s\n",a);
        return 0;
}


January 28, 2003
"Andrew Edwards" <aedwards@spamfreeamerica.com> escribió en el mensaje
news:b16lrf$to4$1@digitaldaemon.com...
| "Carlos Santander B." <carlos8294@msn.com> wrote in message
| news:b16l6l$t8p$1@digitaldaemon.com...
| > It doesn't pass the scanf(). Why?
|
| try this:
|
| import c.stdio;
| int main(char[][] args) {
|         char[] a;
|         char[80] b;
|         printf('hey: ');
|         ubyte n=scanf('%s',&b);
|         printf('.');
|         a = b;
|         printf("%.*s\n",a);
|         return 0;
| }
|
|

Thanks, that worked. I thought that 'char[80] x' was almost like 'char *x'...

—————————————————————————
Carlos Santander
http://carlos3.netfirms.com/


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 2003-01-27


January 28, 2003
"Andrew Edwards" <aedwards@spamfreeamerica.com> escribió en el mensaje
news:b16lrf$to4$1@digitaldaemon.com...
| "Carlos Santander B." <carlos8294@msn.com> wrote in message
| news:b16l6l$t8p$1@digitaldaemon.com...
| > It doesn't pass the scanf(). Why?
|
| try this:
|
| import c.stdio;
| int main(char[][] args) {
|         char[] a;
|         char[80] b;
|         printf('hey: ');
|         ubyte n=scanf('%s',&b);
|         printf('.');
|         a = b;
|         printf("%.*s\n",a);
|         return 0;
| }
|
|

Actually, it doesn't work. I hadn't checked, but when I printf 'b', it doesn't print what I wrote.

—————————————————————————
Carlos Santander
http://carlos3.netfirms.com/


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 2003-01-27


January 28, 2003
> Actually, it doesn't work. I hadn't checked, but when I printf 'b', it doesn't print what I wrote.
>
b is similar to a C or C++ array, to access its content you use a loop. D is simpler though, you can type this

    printf("%.*s\n", b[0 .. b.length]);

your approach  printf("%.*s\n", b) passes the address of the first memory
location to printf and is therefore analagous to printf("%.*s\n", b[0]); "A
request to print the first character of the array."


January 29, 2003
"Andrew Edwards" <aedwards@spamfreeamerica.com> escribió en el mensaje
news:b16lrf$to4$1@digitaldaemon.com...
| "Carlos Santander B." <carlos8294@msn.com> wrote in message
| news:b16l6l$t8p$1@digitaldaemon.com...
| > It doesn't pass the scanf(). Why?
|
| try this:
|
| import c.stdio;
| int main(char[][] args) {
|         char[] a;
|         char[80] b;
|         printf('hey: ');
|         ubyte n=scanf('%s',&b);
|         printf('.');
|         a = b;
|         printf("%.*s\n",a);
|         return 0;
| }
|
|

After trying a lot without any success, I was forced to write my own function, and read a 'char[]' char by char.

—————————————————————————
Carlos Santander
http://carlos3.netfirms.com/


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 2003-01-27


January 29, 2003
Carlos Santander B. wrote:
> "Andrew Edwards" <aedwards@spamfreeamerica.com> escribió en el mensaje
> news:b16lrf$to4$1@digitaldaemon.com...
> | "Carlos Santander B." <carlos8294@msn.com> wrote in message
> | news:b16l6l$t8p$1@digitaldaemon.com...
> | > It doesn't pass the scanf(). Why?
> |
> | try this:
> |
> | import c.stdio;
> | int main(char[][] args) {
> |         char[] a;
> |         char[80] b;
> |         printf('hey: ');
> |         ubyte n=scanf('%s',&b);
> |         printf('.');
> |         a = b;
> |         printf("%.*s\n",a);
> |         return 0;
> | }
> |
> |
> 
> After trying a lot without any success, I was forced to write my own
> function, and read a 'char[]' char by char.

You don't need that.  What's happening is that D arrays have both a length and a pointer, even when they have static length, so you just need to cast it to a pointer to get rid of that:

    scanf ("%s", (char *) b);

Another thing.  The line:

   a = b;

Won't have the effect you want; "a" will have a length of 80.  Use:

    import string;

    ...
    a = string.toString ((char *) b);

Instead.

January 29, 2003
"Burton Radons" <loth@users.sourceforge.net> escribió en el mensaje
news:b17f4s$7a0$1@digitaldaemon.com...
|
| You don't need that.  What's happening is that D arrays have both a
| length and a pointer, even when they have static length, so you just
| need to cast it to a pointer to get rid of that:
|
|      scanf ("%s", (char *) b);
|

I also tried that.

| Another thing.  The line:
|
|     a = b;

I'm not doing that...

|
| Won't have the effect you want; "a" will have a length of 80.  Use:
|
|      import string;
|
|      ...
|      a = string.toString ((char *) b);
|
| Instead.
|

... I do this: a=b[0..strlen(b)-1];

-------------------------
Carlos Santander
http://carlos3.netfirms.com/


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 2003-01-27


January 29, 2003
This whole problem makes me believe that there should be a direct way to read char[] from the console. It has been requested before, and I think Pavel got scanf() to read that (not sure, though), but DMD should include it.

—————————————————————————
Carlos Santander
http://carlos3.netfirms.com/


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 2003-01-27


January 29, 2003
Carlos Santander B. wrote:
> "Burton Radons" <loth@users.sourceforge.net> escribió en el mensaje
> news:b17f4s$7a0$1@digitaldaemon.com...
> |
> | You don't need that.  What's happening is that D arrays have both a
> | length and a pointer, even when they have static length, so you just
> | need to cast it to a pointer to get rid of that:
> |
> |      scanf ("%s", (char *) b);
> |
> 
> I also tried that.

No you didn't, or you had bad code at another part of the program.

> | Won't have the effect you want; "a" will have a length of 80.  Use:
> |
> |      import string;
> |
> |      ...
> |      a = string.toString ((char *) b);
> |
> | Instead.
> |
> 
> ... I do this: a=b[0..strlen(b)-1];

This will lop off the last byte; slice parameters aren't inclusive.

Here's your code, corrected:

import c.stdio, string;
void main() {
        char[] a;
        char[80] b;
        printf('hey: ');
        scanf('%s',(char*)b);
        printf('.');
        a=toString(b);
        printf('%.*s'\n,a);
}

« First   ‹ Prev
1 2