“坏表达”声明char数组c

时间:2012-12-08 01:37:38

标签: c turbo-c

当我使用bcc编译我的.c文件时,我得到一个错误的表达式错误(如下所示),我不知道是什么导致它。

在main中,以下行导致......

    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 个答案:

答案 0 :(得分:1)

有很多关于将指针赋给指针的事情的警告;但唯一的编译错误是您在;之后遗漏了i = j = p

test或附近的声明没有任何错误。

答案 1 :(得分:0)

只是在黑暗中拍摄,但你是否在8位size_t的机器上?如果是,13312将因溢出而导致0。所以实际上,你有:

char test[0];

这将解释错误。请显示输出:

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