Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 21, 2002 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; } |
November 22, 2002 Re: Access Violation (WinXP) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | 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:
> 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;
> }
>
>
|
November 22, 2002 Re: Access Violation (WinXP) | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | what about
printf("%d %.*s around\n", i, t);
as advised in the documentation ?
J C Calvarese wrote:
> 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:
>
> > 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;
> > }
> >
> >
>
|
November 22, 2002 Re: Access Violation (WinXP) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lloyd Dupont | 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:
> what about
> printf("%d %.*s around\n", i, t);
> as advised in the documentation ?
|
Copyright © 1999-2021 by the D Language Foundation