Jump to content

I'm Getting A Syntax Error; Anyone Know What's Wrong?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I tried compiling a file with the following code (what I pasted starts from line 22):
int main ( int argc, char * argv[] ){ 

	HANDLE fh; 

	OFSTRUCT o_s; 

	register char * a; 

	unsigned long int i1; 

	for (a= &o_s; a < (&o_s + sizeof(o_s)); a++) *a= 0; 

	fh= OpenFile ( "C:/test.txt", &o_s, OF_READWRITE ); 

	WriteFile ( fh, "file_io.c was here", sizeof ("file_io.c was here"), &i1, NULL ); 

	CloseHandle ( fh ); 

	return sizeof ("file_io.c was here") - i1; 

} 
But the compiler keeps complaining about types:
C:\Users\Administrator\Desktop>\dm\bin\dmc -I\dm\stlport\stlport file_io.c

        for (a= &o_s; a < (&o_s + sizeof(o_s)); a++) *a= 0;

                    ^

file_io.c(27) : Error: need explicit cast to convert

from: struct _OFSTRUCT*

to  : char *

        fh= OpenFile ( "C:/test.txt", &o_s, OF_READWRITE );

                                                          ^

file_io.c(28) : Error: need explicit cast to convert

from: int __import

to  : void *

--- errorlevel 1

I tried changing the code to this:
int main ( int argc, char * argv[] ){ 

	HANDLE fh; 

	OFSTRUCT o_s; 

	register char * a; 

	unsigned long int i1; 

	for (a= &[COLOR="red"](char *)[/COLOR]o_s; a < (&o_s + sizeof(o_s)); a++) *a= 0; 

	fh= OpenFile ( "C:/test.txt", &o_s, OF_READWRITE ); 

	WriteFile ( fh, "file_io.c was here", sizeof ("file_io.c was here"), &i1, NULL ); 

	CloseHandle ( fh ); 

	return sizeof ("file_io.c was here") - i1; 

} 
But now the compiler tells me:
C:\Users\Administrator\Desktop>\dm\bin\dmc -I\dm\stlport\stlport file_io.c

        for (a= &(char *)o_s; a < (&o_s + sizeof(o_s)); a++) *a= 0;

                            ^

file_io.c(27) : Error: illegal cast

from: struct _OFSTRUCT

to  : char *

        fh= OpenFile ( "C:/test.txt", &o_s, OF_READWRITE );

                                                          ^

file_io.c(28) : Error: need explicit cast to convert

from: int __import

to  : void *

--- errorlevel 1

I don't know if there is anything else wrong with the code, though, besides the syntax.

But I don't know what the compiler means; can anyone tell me how to fix the code?

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Okay, I got it to compile by using inline assembler; but can't C be good enough to do a simple thing?

Now the problem is it crashes with a C0000005 exception.

The code (once again, starting from line 22):
int main ( int argc, char * argv[] ){ 

	HANDLE fh; 

	OFSTRUCT o_s; 

	//register char * a; 

	unsigned long int i1; 

	asm { 

		mov ebx, offset o_s 

		xor ecx, ecx 

		lp1: 

			cmp ecx, sizeof o_s 

			jnl lp1s 

			

			mov byte ptr [ebx+ecx], 0 

			inc ecx 

			jmp lp1 

		lp1s: 

	} 

	fh= (void *)OpenFile ( "C:/test.txt", &o_s, OF_READWRITE ); 

	WriteFile ( fh, "file_io.c was here", sizeof ("file_io.c was here"), &i1, NULL ); 

	CloseHandle ( fh ); 

	return sizeof ("file_io.c was here") - i1; 

} 

That's one of the reasons I don't like using inline assembler; it doesn't really work as well as plain .asm assembler (it doesn't work whenever I try it, anyway). But C doesn't exactly do the job right, either.

It doesn't make sense to me; the code looks like it should work. Is there anything I'm missing?

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Okay, I figured out what it is about the inline assembly.

The problem was that I used the 'offset' keyword, while the variable I was " 'offset'-ing " was a local variable, but 'offset' only works on global variables. The right thing to do was to use the LEA instruction, to load the address of 'o_s' into register EBX.

int main ( int argc, char * argv[] ){ 

	HANDLE fh; 

	OFSTRUCT o_s; 

	//register char * a; 

	unsigned long int i1; 

	asm { 

		[COLOR="red"]lea ebx, [o_s] [/COLOR]

		xor ecx, ecx 

		lp1: 

			cmp ecx, sizeof o_s 

			jnl lp1s 

			

			mov byte ptr [ebx+ecx], 0 

			inc ecx 

			jmp lp1 

		lp1s: 

	} 

	fh= (void *)OpenFile ( "C:/test.txt", &o_s, OF_READWRITE ); 

	WriteFile ( fh, "file_io.c was here", sizeof ("file_io.c was here"), &i1, NULL ); 

	CloseHandle ( fh ); 

	return sizeof ("file_io.c was here") - i1; 

} 

It works now.

Though I still don't understand why the compiler was giving me errors before.

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
About the type, here was my attempt:
	OFSTRUCT o_s; 

	register char * a; 

	unsigned long int i1; 

	for (a= [COLOR="darkorange"]&(char *)o_s[/COLOR]; a < (&o_s + sizeof(o_s)); a++) *a= 0; 

So what is the right way to make 'o_s', in '&o_s', look like it's a 'char' type variable, and not an 'OFSTRUCT' type variable?

The way I tried doesn't work, for some reason.

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
for (a = (char *) (&o_s); a < (&o_s + sizeof(o_s)); a++) *a = 0;
?

Why not just use memset?
memset(&o_s, 0, sizeof(o_s));

Wow I changed my sig!

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Thanks.

This works now:
int main ( int argc, char * argv[] ){ 

	HANDLE fh; 

	OFSTRUCT o_s; 

	register char * a; 

	unsigned long int i1; 

	for (a= (char *)(&o_s); a < ((char *)&o_s + sizeof(o_s)); a++) *a= 0; 

	fh= (void *)OpenFile ( "C:/test.txt", &o_s, OF_READWRITE ); 

	WriteFile ( fh, "file_io.c was here", sizeof ("file_io.c was here"), &i1, NULL ); 

	CloseHandle ( fh ); 

	return sizeof ("file_io.c was here") - i1; 

} 

memset() is a nice idea, as well.

But thanks again for the syntax help.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users