Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

syntax syntax error

  • Please log in to reply
3 replies to this topic

#1 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 26 June 2011 - 06:40 PM

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?
  • 0

#2 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 26 June 2011 - 11:00 PM

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?
  • 0

#3 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 27 June 2011 - 12:51 AM

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.
  • 0

#4 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 27 June 2011 - 07:14 PM

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.
  • 0





Also tagged with one or more of these keywords: syntax, syntax error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download