语法错误:缺少';'在'类型'IN C之前

时间:2013-09-20 09:10:21

标签: c visual-studio-2010 visual-studio

我试图完成我的代码。之后为数组分配内存。我得到的错误很少,因为他们我无法编译代码。 我想得到你的帮助来完成这个项目,谢谢!

1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(27): error C2143: syntax error : missing ';' before 'type'
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(28): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(28): warning C4047: '=' : 'int' differs in levels of indirection from 'char **'
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(29): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(36): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(36): error C2109: subscript requires array or pointer type
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(38): error C2143: syntax error : missing ';' before 'type'
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(47): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(47): error C2109: subscript requires array or pointer type
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(47): error C2198: 'gets' : too few arguments for call
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(50): error C2065: 'text2' : undeclared identifier
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(50): warning C4047: 'function' : 'char **' differs in levels of indirection from 'int'
1>c:\users\roni\documents\visual studio 2010\projects\lb11_12\lb11_12\lb_11_12_02.c(50): warning C4024: 'PrintStr2' : different types for formal and actual parameter 1
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是我的代码

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define LINES 4
#define length 100

void SortText1(char **text1);
void SortText2(char **text2);
void PrintStr1(char **text1);
void PrintStr2(char **text2);
void main (){
    int i;
    char **text1;
    text1=(char **)malloc(LINES*sizeof(char *));    
    if(!text1){ // Terms - if there is not enough memory,print error msg and exit the program.  
        printf("alloc failed\n");
        return ;
    }

    for (i=0 ; i<LINES; i++){
        text1[i] =(char*) malloc(length*sizeof(char));
    }

    char **text2;
    text2=(char **)malloc(LINES*sizeof(char *));
    if(!text2){ // Terms - if there is not enough memory,print error msg and exit the program.  
        printf("alloc failed\n");
        return ;
    }

    for (i=0 ; i<LINES; i++){
        text2[i] =(char*) malloc(length*sizeof(char));
    }

    char text3[LINES+LINES][length];
    printf("First string,Please enter %d names (only capital letters,without spaces):", LINES);
    for ( i=0 ; i<LINES ; i++){
        gets(text1[i]);
    }

    printf("Second string,Please enter %d names (only capital letters,without spaces):", LINES);
    for ( i=0 ; i<LINES ; i++){
        gets(text2[i]);
    }

    PrintStr1(text1);
    PrintStr2(text2);
    SortText1(text1);
    system("pause");
    return ;
}
void SortText1(char **text1){   
    int i,j,k;
    for (i=LINES-1 ; i>0 ; i--){
        for ( j=0 ; j<i ; j++ ){
            if(strcmp(text1[i],text1[i+1]) > 0 ){
                char *temp=text1[i+1];
                text1[i+1]=text1[i];
                text1[i]=temp;
            }
        }
    }

    printf("The first string sorted:\n");
    for ( k=0 ; i<LINES ; k++){
        printf("%s ",text1[k]);
    }
    printf("\n");
}

void PrintStr1(char **text1){
    int i;
    printf("The first string of NAMES:\n");
    for ( i=0 ; i<LINES ; i++){
        printf("%s ",text1[i]);
    }
    printf("\n");
}
void PrintStr2(char **text2){
    int i;
    printf("The second string of NAMES:\n");
    for ( i=0 ; i<LINES ; i++){
        printf("%s ",text2[i]);
    }
    printf("\n");
}

6 个答案:

答案 0 :(得分:6)

您收到这些错误是因为您声明的本地变量不在范围的开头。

     for (i=0 ; i<LINES; i++)
     {
        text1[i] =(char*) malloc(length*sizeof(char));
     }
   char **text2; // <<<<<<<<<<<<<<<<<<<
    text2=(char **)malloc(LINES*sizeof(char *));

在C99标准中首次引入了在范围中间声明变量的功能,MS编译器不支持。

将所有局部变量声明移到{之后以及它们所用的块中的任何其他语句之前。

答案 1 :(得分:2)

可以肯定的是,仅在函数体的开头指定局部变量。否则,C编译器可能会给你一个编译错误。

答案 2 :(得分:2)

您的编译器似乎使用旧版本的C标准。它不允许在块的中间声明。您需要移动所有声明,以便它们都在块的第一个可执行语句之前。

答案 3 :(得分:2)

在C89中,您只能在块的开头放置变量声明。此限制已在C99中删除。

但Visual Studio 2010尚不支持C99。所以你必须坚持C89风格:

int main ()        //use int main()
{
    int i;
    char **text1;
    char **text2;  //Put all the declarations in the beginning
    // ...
    return 0;
}

答案 4 :(得分:1)

C的旧标准不允许你这样做(C ++和更新的C标准都这样做)。你可以通过引入一个新的身体来使用它:

void function()
{
    int i;

    i = 0;

    // Do some more things
    {
        int AnotherInt = 33;
        AnotherInt += i; // Can still reference i here
    }

    // AnotherInt is now "out of scope" and cannot be referenced any more
}

答案 5 :(得分:0)

在ansi C中,即c89,你必须先在函数中声明所有变量。

主要应改为

void main ()
{
    int i;
    char **text1;
    char **text2;
    char text3[LINES+LINES][length];

    // put the rest of your code here

}

如果要在代码中间声明变量,则必须使用c99(或者您的编译器可能还有其他选项)。

/此后的一切都是挑剔。

作为一种风格,你应该像#define常量一样大写长度。 你应该声明你的函数是静态的,这样它们就不会污染全局命名空间:

static void SortText1(char **text1);
static void SortText2(char **text2);
static void PrintStr1(char **text1);
static void PrintStr2(char **text2);

Main应该是int main(int argv,char ** argc)而不是void main() 除非编译器没有正确定义标准库,否则不需要包含malloc.h和conio.h。

for ( k=0 ; i<LINES ; k++)

应将i更改为k。

缩进不一致。

另外,您应该一次编写一小段程序。编写许多函数然后尝试编译它们会导致这样的问题。如果你正在复制别人的家庭作业,那么当然你只是在编写一些失败的大页面,但是如果你自己编写这个,那么在编辑之间使用短代码将会快得多。

/ end nitpicking

祝你的项目好运。