2012-12-08 38 views
-1

當我使用bcc編譯我的.c文件時,我得到一個不良的表達式錯誤(如下所示),我不知道是什麼導致它。「bad expression」聲明char數組c

在主,以下行導致...

char test[13312]; 

...此錯誤

kernel.c:25.5: error: bad expression 
kernel.c:25.10: error: need ';' 

評論指出,線(和使用該測試陣列線)使誤差去遠。

這是所有的代碼:

/* kernel 
* 
* Register parameters for various BIOS interrupts 
* http://www.ctyme.com/intr/int.htm 
* 
* Sector 1: Map 
* Sector 2: Dir 
* Sector 3: Kernel 
*/ 

void printString(char* chars); 
void readString(char* arr); 
void readSector(char* buffer, int sector); 
void readFile(char* fileName, char* buffer); 

void handleInterrupt21(int ax, int bx, int cx, int dx); 

int mod(int a, int b); 
int div(int a, int b); 

void main(){ 
    printString("helloooo from main"); 
    //char line[80]; 
    //char buffer[80]; 
    char test[13312]; 
    makeInterrupt21(); 
    interrupt(0x21,0,"test",0,0); 
    //interrupt(0x21,3,"messag",buffer,0); 
    //interrupt(0x21,3,"helloo",line,0); 
    //interrupt(0x21,0,"hello from main",0,0); 
    readFile("messag",test);  
    interrupt(0x21,0,test,0,0); 
    //interrupt(0x21,0,line,0,0); 
    while(1); 
} 


// Prints out each character of the array until it reaches 0x0 
// Call using Int 21 w/ ax=0 
void printString(char* chars){ 

    int i=0; 

    for(i=0;chars[i]!='\0';i++){ 
     char al = chars[i]; 
     char ah = 0xe; 
     int ax = ah * 256 + al; 
     interrupt(0x10, ax, 0, 0, 0); 
    } 

} 

// Reads from keyboard, while updating the display with what is being typed 
// Can deal with backspaces 
// Call using Int 21 w/ ax=1 
void readString(char* arr){ 
    char enter = 0xd; 
    char endLine = 0xa; 
    char nullChar = 0x0; 
    char back = 0x8; 

    // while loop set up 
    int i = 0; 
    char ascii = interrupt(0x16, 0, 0, 0, 0); 
    interrupt(0x10, 0xe*256+ascii, 0, 0, 0); 

    // exit key: enter 
    while(ascii!=enter){ 

     // decrements i (up to 0) if a backspace is entered, otherwise increments i 
     // deals with what is in the char array   
     if(ascii==back&&i>0) {i--;} 
     else if(ascii==back) {i=0;} 
     else {arr[i]=ascii; i++;} 

     // get next input letter and write it to screen 
     ascii = interrupt(0x16, 0, 0, 0, 0); 
     interrupt(0x10, 0xe*256+ascii, 0, 0, 0); 

     // clear the display when backspace is clicked 
     if(ascii==back){ 
      interrupt(0x10, 0xe*256+nullChar, 0, 0, 0); 
      interrupt(0x10, 0xe*256+ascii, 0, 0, 0); 
     } 
    } 

    // puts end line and null characters at the end of the array 
    arr[i] = endLine; 
    arr[i+1] = nullChar; 


    // Writes a new line character to the screen 
    interrupt(0x10, 0xe*256+endLine, 0, 0, 0); 
} 

// Will take a predefined character array of 512 bytes+, and a sector number 
// Call using Int 21 w/ ax=2 
void readSector(char* buffer, int sector){ 
    int ah = 2; // tells BIOS to read 
    int al = 1; // number of sectors to read 
    int ax = ah * 256 + al; 
    int bx = buffer; // address where the data should be stored to 
    int ch = div(sector,36); //0 // track number 
    int cl = mod(sector,18)+1; //13; // relative sector number 
    int cx = ch * 256 + cl;  
    int dh = mod(div(sector,18),2); //1; // head number 
    int dl = 0; // device number; 0=floppy 
    int dx = dh * 256 + dl; 

    //printString(bx); 

    interrupt(0x13, ax, bx, cx, dx); 
    //printString((char)buffer); 
} 

// Call using Int 21 w/ ax=3 
// Not a finished 
void readFile(char* fileName, char* buffer){ 
    printString("entered"); 
    char dir[512]; 
    readSector(dir,2); 
    // p will always be first char of what we're looking at 
    int i,j,p; 
    i = j = p 
    int flag= 0;  

    while(i<16){ 
     printString("i-loop"); 
     j=p; 
     while(j<6){ 
      printString("j-loop"); 
      if(!dir[j]==fileName[j-p]) 
       break; 
      j++; 
     } 
     if(j==5) {flag=1; printString("flag=1"); break;} 
     p+=32; 
     i++; 
    } 
    if(flag==0){ 
     printString("0"); 
    } else { 
     printString("1"); 
    } 

    buffer = flag; 
} 

void handleInterrupt21(int ax, int bx, int cx, int dx){ 
    if(ax==0) { 
     // bx = String 
     printString(bx); 
    } else if(ax==1) { 
     // bx = buffer to hold read string 
     readString(bx); 
    } else if(ax==2) { 
     // bx = buffer to hold read string 
     // cx = sector number 
     readSector(bx,cx); 
    } else if(ax==3) { 
     // bx = char array holding file name 
     // cx = address of a buffer to hold the file 
     readFile(bx,cx);  
    } else { 
     printString("Invalid use of Int 21\0"); 
    } 
} 

// aka a%b 
int mod(int a, int b){ 
    while(a>=b) 
     a=a-b; 
    return a; 
} 

// aka a/b 
int div(int a, int b){ 
    int q = 0; 
    while (a>=b){ 
     q++; 
     a=a-b; 
    } 
    return q; 
} 



更新: 固定申報錯誤仍然導致編譯錯誤,(只是BCC)。似乎編譯器不喜歡任何變量聲明。

/* kernel 
    * 
    * Register parameters for various BIOS interrupts 
    * http://www.ctyme.com/intr/int.htm 
    * 
    * Sector 1: Map 
    * Sector 2: Dir 
    * Sector 3: Kernel 
    */ 

    void printString(char* chars); 
    void readString(char* arr); 
    void readSector(char* buffer, int sector); 
    void readFile(char* fileName, char* buffer); 

    void handleInterrupt21(int ax, int bx, int cx, int dx); 

    int mod(int a, int b); 
    int div(int a, int b); 

    void main(){ 
     //char line[80]; 
     //char buffer[80]; 
     char test[13312]; 
     makeInterrupt21(); 
     interrupt(0x21,0,"test",0,0); 
     //interrupt(0x21,3,"messag",buffer,0); 
     //interrupt(0x21,3,"helloo",line,0); 
     //interrupt(0x21,0,"hello from main",0,0); 
     readFile("messag",test);  
     interrupt(0x21,0,test,0,0); 
     //interrupt(0x21,0,line,0,0); 
     while(1); 
    } 


    // Prints out each character of the array until it reaches 0x0 
    // Call using Int 21 w/ ax=0 
    void printString(char* chars){ 

     int i=0; 

     for(i=0;chars[i]!='\0';i++){ 
      char al = chars[i]; 
      char ah = 0xe; 
      int ax = ah * 256 + al; 
      interrupt(0x10, ax, 0, 0, 0); 
     } 

    } 

    // Reads from keyboard, while updating the display with what is being typed 
    // Can deal with backspaces 
    // Call using Int 21 w/ ax=1 
    void readString(char* arr){ 
     char enter = 0xd; 
     char endLine = 0xa; 
     char nullChar = 0x0; 
     char back = 0x8; 

     // while loop set up 
     int i = 0; 
     char ascii = interrupt(0x16, 0, 0, 0, 0); 
     interrupt(0x10, 0xe*256+ascii, 0, 0, 0); 

     // exit key: enter 
     while(ascii!=enter){ 

      // decrements i (up to 0) if a backspace is entered, otherwise increments i 
      // deals with what is in the char array   
      if(ascii==back&&i>0) {i--;} 
      else if(ascii==back) {i=0;} 
      else {arr[i]=ascii; i++;} 

      // get next input letter and write it to screen 
      ascii = interrupt(0x16, 0, 0, 0, 0); 
      interrupt(0x10, 0xe*256+ascii, 0, 0, 0); 

      // clear the display when backspace is clicked 
      if(ascii==back){ 
       interrupt(0x10, 0xe*256+nullChar, 0, 0, 0); 
       interrupt(0x10, 0xe*256+ascii, 0, 0, 0); 
      } 
     } 

     // puts end line and null characters at the end of the array 
     arr[i] = endLine; 
     arr[i+1] = nullChar; 


     // Writes a new line character to the screen 
     interrupt(0x10, 0xe*256+endLine, 0, 0, 0); 
    } 

    // Will take a predefined character array of 512 bytes+, and a sector number 
    // Call using Int 21 w/ ax=2 
    void readSector(char* buffer, int sector){ 
     int ah = 2; // tells BIOS to read 
     int al = 1; // number of sectors to read 
     int ax = ah * 256 + al; 
     int bx = buffer; // address where the data should be stored to 
     int ch = div(sector,36); //0 // track number 
     int cl = mod(sector,18)+1; //13; // relative sector number 
     int cx = ch * 256 + cl;  
     int dh = mod(div(sector,18),2); //1; // head number 
     int dl = 0; // device number; 0=floppy 
     int dx = dh * 256 + dl; 

     //printString(bx); 

     interrupt(0x13, ax, bx, cx, dx); 
     //printString((char)buffer); 
    } 

    // Call using Int 21 w/ ax=3 
    // Not finished 
    void readFile(char* fileName, char* buffer){ 
     printString("entered"); 
     char dir[512]; 
     readSector(dir,2); 
     // p will always be first char of what we're looking at 
     int i,j,p; 
     i = j = p = 0; 
     while(i<16){ 
      printString("i-loop"); 
      j=p; 
      while(j<6){ 
       printString("j-loop"); 
       if(!dir[j]==fileName[j-p]) 
        break; 
       j++; 
      } 
      if(j==5) {flag=1; printString("flag=1"); break;} 
      p+=32; 
      i++; 
     } 
     if(flag==0){ 
      printString("0"); 
     } else { 
      printString("1"); 
     } 

     buffer = flag; 
    } 

    void handleInterrupt21(int ax, int bx, int cx, int dx){ 
     if(ax==0) { 
      // bx = String 
      printString(bx); 
     } else if(ax==1) { 
      // bx = buffer to hold read string 
      readString(bx); 
     } else if(ax==2) { 
      // bx = buffer to hold read string 
      // cx = sector number 
      readSector(bx,cx); 
     } else if(ax==3) { 
      // bx = char array holding file name 
      // cx = address of a buffer to hold the file 
      readFile(bx,cx);  
     } else { 
      printString("Invalid use of Int 21\0"); 
     } 
    } 

    // aka a%b 
    int mod(int a, int b){ 
     while(a>=b) 
      a=a-b; 
     return a; 
    } 

    // aka a/b 
    int div(int a, int b){ 
     int q = 0; 
     while (a>=b){ 
      q++; 
      a=a-b; 
     } 
     return q; 
    } 

錯誤:

kernel.c:119.6: error: bad expression 
kernel.c:119.10: error: need ';' 
kernel.c:119.11: error: dir undeclared 
kernel.c:119.15: error: illegal indirection 
kernel.c:122.5: error: bad expression 
kernel.c:122.7: error: need ';' 
kernel.c:122.8: error: i undeclared 
kernel.c:122.10: error: j undeclared 
kernel.c:122.12: error: p undeclared 
kernel.c:129.14: error: illegal indirection 
kernel.c:133.18: error: flag undeclared 
+2

您在'i = j = p'上缺少';',但這可能不是問題。 – imreal

+0

@尼克爲什麼這可能不成問題? –

+3

@ shiplu.mokadd.im它是* a *的問題,但它可能不是* *的問題;-) –

回答

1

有這樣的事情分配整數指針許多警告;但唯一的編譯錯誤是您在i = j = p之後缺少;

test或其附近沒有任何錯誤。

+0

這是一個很好的接口,但是,更改聲明仍然會留下一些編譯錯誤(請參閱上文)。 – user1886983

0

只是在黑暗中拍攝,但你是在一臺機器上與8位size_t?如果是,13312將由於溢出而導致0。所以實際上,你有:

char test[0]; 

這將解釋錯誤。請輸出:

printf("%u\n", (unsigned)sizeof(size_t)); 
+0

符合的實現不能使'size_t'小。我在'sizeof'表達式中添加了一個轉換,以便類型匹配。 –

+0

@凱瑟湯普森嗯,的確如此。雖然13312結果完全在0的情況下,當輸入到「uint8_t」或「int8_t」時仍然會讓我感到困惑。非常巧合。 –

+0

13312是0x3400;在編程方面,它是一個整數。 –