|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D - Access Violation (WinXP)
The following compiles and links sucessfully but causes a runtime "access
violation" error while trying to access t. Any ideas?
int main()
{
char[] t;
for(int i=0; i < 10; i++){
if(i==0) t="time"; else t="times";
printf("%d %.s around\n", i, t);
}
return 0;
}
This version doesn't increment i before iterating through the loop:
int main()
{
char[] t;
for(int i=0; i <= 10; ++i){
if(i==1) t="time"; else t="times";
printf("%d %.s around\n", i, t);
}
return 0;
}
Nov 21 2002
Andrew,
I casted t to a char*.
printf("%d %.s around\n", i, cast(char*) t);
This got rid of the access violation, but "time(s)" didn't print.
Also, I though maybe a null needed to be appended as shown below, but it
didn't work any better.
printf("%d %.s around\n", i, cast(char*) (t ~ \x00));
Finally I imported strings from Phobos and used toString for converting
i to a string. This seemed to work, but I suspect there's another (more
direct) solution.
import string;
int main()
{
char[] t;
for(int i=0; i < 10; i++){
if(i==0) t="time"; else t="times";
printf(toString(i) ~ " " ~ t ~ " around\n");
}
return 0;
}
Justin
Andrew Edwards wrote:
Nov 21 2002
what about
printf("%d %.*s around\n", i, t);
as advised in the documentation ?
J C Calvarese wrote:
Nov 21 2002
I guess I forgot to read the directions. Thanks. I knew there was a direct solution. I haven't worked much in C so all of these printf codes (%d, %s, etc.) are kind of foreign to me. Justin Lloyd Dupont wrote: Nov 21 2002
|