D - scanf, char*, access violation
- Carlos Santander B. (23/23) Jan 28 2003 This compiles correctly:
 - Andrew Edwards (14/15) Jan 28 2003 try this:
 - 
Carlos Santander B.
 (30/30)
 Jan 28 2003
 "Andrew Edwards" 
escribiσ en el mensaje  - 
Carlos Santander B.
 (30/30)
 Jan 28 2003
 "Andrew Edwards" 
escribiσ en el mensaje  - Andrew Edwards (6/8) Jan 28 2003 b is similar to a C or C++ array, to access its content you use a loop. ...
 - 
Carlos Santander B.
 (30/30)
 Jan 28 2003
 "Andrew Edwards" 
escribiσ en el mensaje  - Burton Radons (12/36) Jan 28 2003 You don't need that. What's happening is that D arrays have both a
 - 
Carlos Santander B.
 (32/32)
 Jan 28 2003
 "Burton Radons" 
escribiσ en el mensaje  - Carlos Santander B. (11/11) Jan 28 2003 This whole problem makes me believe that there should be a direct way to
 - Burton Radons (14/36) Jan 28 2003 This will lop off the last byte; slice parameters aren't inclusive.
 - 
Carlos Santander B.
 (22/22)
 Jan 28 2003
 "Burton Radons" 
escribiσ en el mensaje  
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
 Jan 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; }
 Jan 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
 Jan 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
 Jan 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."
 Jan 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;
| }
|
|
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
 Jan 28 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.
 Jan 28 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
 Jan 28 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
 Jan 28 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); }
 Jan 28 2003
"Burton Radons" <loth users.sourceforge.net> escribiσ en el mensaje
| 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);
| }
|
Thanks
-------------------------
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
 Jan 28 2003








 
 
 
 "Carlos Santander B." <carlos8294 msn.com> 