Unknown compiler error

This is a discussion on Unknown compiler error within the C Programming forums, part of the General Programming Boards category; Ok, i tried to compile this code: Code: int SetPixel(int x, int y, int color); int main(void) { typedef unsigned ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    4

    Unknown compiler error

    Ok, i tried to compile this code:

    Code:
    int SetPixel(int x, int y, int color); 
    
    int main(void) {
    typedef unsigned char byte;
    byte *VGA = (byte *)0xA0000; //This gives you a pointer to the graphics video memory
    
    int x;
    int y;
    for(y=0;y<201;y++) {
    for(x=0;x<321;x++) {
    SetPixel(x,y,x); //Plot a pixel
    }
    
    int SetPixel(int x, int y, int color) {
    VGA[320*y+x]=color; //This plots a pixel at x,y
    };

    I used this command on windows xp with the dev cpp release

    Code:
    gcc -O3 -c kernel_c.c
    and i got this errror:
    (There is no line 17 in the file, the last line ends with the }; and is line 16)

    Code:
    17 F:\Documents and Settings\chris\Desktop\Knaut\kernel_c.c
    parse error at end of input
    this is an important part of an operating system im writing, and it would be really great if someone knew how to fix this.. I am a java coder with a little c++ experince, but no c experince.

    Thank you sooooo much!!

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You don't need a semicolon after the closing brace of the SetPixel function. Also, you are missing a closing brace at the end of main( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Unknown compiler error

    You never closed your for loop scopes. Either add 2 end braces after you set the pixel, or get rid of the two open braces (both will work).

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    Wow, and all those times my teacher told me how important indentation is, i ignored her... I feel really stupid now, thanks.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    int SetPixel(int x, int y, int color); 
    
    int main(void) {
    typedef unsigned char byte;
    byte *VGA = (byte *)0xA0000; //This gives you a pointer to the 
      //graphics video memory
    
    int x;
    int y;
    for(y=0;y<201;y++) {
    for(x=0;x<321;x++) {
    SetPixel(x,y,x); //Plot a pixel
    }
    
    int SetPixel(int x, int y, int color) {
    VGA[320*y+x]=color; //This plots a pixel at x,y
    };
    Looks better as:
    Code:
    int SetPixel(int x, int y, int color); 
    typedef unsigned char BYTE;
    
    int main(void) 
    {
      //Don't typedef here - only valid in main
      //This gives you a pointer to the graphics video memory  
      BYTE far *VGA = (BYTE far *)0xA0000; //This gives you a pointer 
      
      // ** Alternate far pointer creation  **
      //MK_FP is in dos.h, if you have it  
      //BYTE far *VGA=(BYTE far *)MK_FP(0,0xA000);  
    
      int x;
      int y;
      for(y=0;y<201;y++) 
      {
         for(x=0;x<321;x++) 
         {
            SetPixel(x,y,x); //Plot a pixel
         }
      }
    }
    
    int SetPixel(int x, int y, int color) 
    {
      VGA[320*y+x]=color; //This plots a pixel at x,y
    }

    But your code is going to be slow if you use it in a game. Each time you call SetPixel a stack frame is setup and then the stack is cleaned up prior to returning to the caller. A better way to do this is:

    Code:
    BYTE *Surface;
    BYTE *Screen;
    BYTE *Buffer;
    #define PIXEL(x,y,c)  (Surface[(y<<6)+(y<<8)+x]=c)
    In this snippet every time you do a PIXEL(x,y,c) the compiler will insert your #define code as is - hence it is not a function call thus saving the cycles needed to setup a stack frame and clean the stack up.

    Surface is a pointer to the current surface. Reason for this is that you may want a double buffer but you want one function that can plot to both. If you simply change the surface pointer to the buffer pointer, you write to the buffer - if you change the surface pointer to point at the screen - you write to the screen. No additional overhead involved - except for the flipping of pointers which is next to nil.

    Also if a section of code is going to be enclosed in braces I always place the braces on separate lines and I indent each section of code 2 spaces. As you can see it would have been easy to spot your error since if your closing brace does not lie on the left margin then you need another brace to close a section of code.
    Using my system of braces, I rarely, if ever, have any brace related problems.
    Last edited by VirtualAce; 12-30-2003 at 09:04 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > byte *VGA = (byte *)0xA0000; //This gives you a pointer to the graphics video memory
    And the chances that this will work on anything which isn't a real-mode DOS compiler (ie, not "I used this command on windows xp with the dev cpp release") is about zero.

    > this is an important part of an operating system im writing
    I'd say you're a couple of years short of some good solid C experience then.

    This will keep you off the streets for a while
    http://www.nondot.org/sabre/os/articles
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21