Last update July 31, 2009

Faq Roadmap

This page lists typical questions from D users. Most of them are simply recorded from the newsgroups without much reworking. There are of two other FAQs you might check:

This page is intended to act an simple authoring system to prepare content for both or any other FAQ someone might want to create. Once the questions receive a more formalized response, they will be moved to an appropriate FAQ location:

Table of contents of this page
Error Messages   
What does a particular error message mean?   
"Error: Access Violation" on printing a string   
Have another question?   

Error Messages    

What does a particular error message mean?    

If it's a common error, it may already be on the Error Messages list.

"Error: Access Violation" on printing a string    

You'll likely get an access violation if you try to print a char[] in this fashion:

printf("%s\n", input);

Use std.stdio.writef instead of printf, and std.stdio.writefln to print an extra newline after your output.

If you must use printf, this would be a correct way to do it:

printf("%.*s\n", input);

In D, arrays store their length, so character arrays need no terminating zero. In printf, the '%s' indicates a zero terminated string, so it fails when given a normal D string. The '%.*s' tells printf to expect a string preceded by its length. However, printf is still a C function, and so if the string contains embedded zero characters, printf will stop printing the string even before the specified length is reached. In other words, the '%.*s' will print until the length of the string is reached, or a zero is reached, whichever happens first.

The following examples will also work (though less efficient, because a temporary zero terminated string object is constructed):

printf(cast(char*) (input ~ "\n\0"));
printf(cast(char*) (input ~ \n ~ \0));
printf("%s\n",(char *)(input ~ \0));
printf("%s\n",(char *)input.dup);

Sometimes this is seen:

printf("%s\n",(char *)input);

but it is not always reliable, e.g. after:

char [] a="abcdefghij";
char [] input=a[3..4];
printf("%s\n",(char *)input);

it will not print "de" but "defghij" because no ending \0 is provided by the slice.

There is a HowTo/printf page giving more information on using printf.

Have another question?    

The best way to get an answer to another question is to post it in one of the D newsgroups.

FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: July 31, 2009 18:58 (diff))