Thread overview
Access Violation (WinXP)
Nov 21, 2002
Andrew Edwards
Nov 22, 2002
J C Calvarese
Nov 22, 2002
Lloyd Dupont
Nov 22, 2002
J C Calvarese
November 21, 2002
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
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
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
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 ?