2012-03-20 67 views
2

基本上,我一直在C编程上做类,在那个类中我们使用Linux机器来编写和编译我们的代码。在Windows 7上从linux到visual studio 2010的c编程问题

下面的程序是我们第一个任务的一部分,我在课堂上编译并在Linux上运行它,但没有问题,但已将它带回家我不能在我的生活中让它在Visual Studio 2010中编译最终的,或者带有MinGW编译器的eclipse IDE。

在导致我的代码失败的两个操作系统之间切换是否存在一些典型问题?或者让我作为新手,写了一些不符合VS 2010或Eclipse的丑陋代码?

尝试修复我从VS 2010中得到的错误消息似乎是徒劳的,所以我倾向于从我的计算机中丢失重要的东西。我也设置VS 2010编译C代码,所以我不认为这是问题。从VS2010

错误:

project1a.c(38):错误C2143:语法错误:缺少 ';'前 '类型'
project1a.c(41):错误C2065:I':未声明的标识符
project1a.c(44):错误C2065:userArray':未声明的标识符
project1a.c(44):错误C2065:I':未声明的标识符
project1a.c(44):错误C2109:下标要求数组或指针类型
project1a.c(51):错误C2065:userArray':未声明的标识符

有是'i'的多个实例:这些错误之间的未声明的标识符错误

#include <stdio.h> 
#include <stdlib.h> 

int n; 
float total, avg; 

int sumavg(void); 

int main(void) 
{ 
    //First time scan for the value to be assigned to n. 
    printf("Hey, Enter a number or 999 to exit:> "); 
    scanf("%d", &n); 

    //if n == 999 then exit the program 
    while(n != 999) 
    { 
     //enter the sumavg function. 
     sumavg(); 

     //Try to run the program again. 
     printf("Hey, Enter a number or 999 to exit:> "); 
     scanf("%d", &n); 
    } 

    //exit program. 
    return EXIT_SUCCESS;  
} 

int sumavg(void) 
{ 


    //Define a number that will be used for the array size. 
    printf("Hey, now enter %d more numbers:>\n", n); 

    //Define the size of array using the number assigned to the variable "n". 
    int userArray[n], i; 

    //Construct the array, one position at a time using the for loop. 
    for (i = 0; i < n; i++) 
    { 
     //Assign a value to userArray[i] while i < n(the size of the array). 
     scanf("%d", &userArray[i]); 
    } 

    //Calculate the sum by looping through each position in the userArray[i]. 
    for (i = 0; i < n; i++) 
    { 
     //Take the current position in the array and add it to the variable: "total" 
     total += userArray[i]; 
    } 

    //Calculate the average 
    avg = total/n; 

    //Print the sum followed by the average 
    printf("Sum is: %.1lf\n", total); 
    printf("The average is: %.1lf\n", avg); 

    //reset total and avg in case future iterations are performed. 
    total = 0; 
    avg = 0; 
} 
+1

您得到的错误是什么? – 2012-03-20 03:27:57

+0

请发布一些编译器警告/错误。 – Mosby 2012-03-20 03:28:40

+0

您可以发布您在编译器中获得的错误消息吗?这将有助于缩小问题的范围。 – Gangadhar 2012-03-20 03:29:03

回答

6

问题当编译C代码时,MSVC不支持C99,只支持C90(除了可能用于少数库的东西)。您至少使用MSVC不支持的两个C99功能:

  • 最大的问题是“可变长度数组”。解决这个问题通常需要对代码进行相当多的修改,如果你以任何重要的方式使用它们的话。我会在稍后回顾。

  • 另一个是后“正常”的语句

C99允许声明在其他类型的语句后的块发生发生声明; C90不允许这么做 - 所有的声明必须发生在一个程序段的开始处。这样的话,你声明userArray例如:

int sumavg(void) 
{ 
    //Define a number that will be used for the array size. 
    printf("Hey, now enter %d more numbers:>\n", n); 

    //Define the size of array using the number assigned to the variable "n". 
    int userArray[n], i; 

    //... 

,是不是在C90许可并在C模式编译(它不会在编译C++,因为这种东西是用C支持时MSVC抱怨它++ )。

要解决这个问题只有一个街区开始后移动你的声明:

int sumavg(void) 
{ 
    //Define the size of array using the number assigned to the variable "n". 
    int userArray[n], i; 

    //Define a number that will be used for the array size. 
    printf("Hey, now enter %d more numbers:>\n", n); 

    //... 

有时会要求您rejigger初始化,什么不是。

要解决使用可变长度数组的问题需要更多的工作。在这种情况下,我认为你可以声明userArray作为int*,它使用malloc()分配存储获得通过:

int* userArray; 

userArray = malloc(sizeof(int) * n); 

一些其他的东西:

  • 因为totalavg没有使用在sumavg()之外,它们应该是局部变量(显式初始化为0)
  • 您可能想要将n作为参数传递给sumavg()而不是使用全局变量
  • 您声明sumavg()返回int,但不返回任何内容。您应该将声明更改为void
+0

啊,谢谢你在这个答案中的深度,肯定帮助我理解出了什么问题! – 2012-03-20 04:32:15

+0

作为一个在Visual Studio中缺乏现代C标准支持的人,我认为这是一个非常好的答案。 – 2012-03-20 08:18:51

-1

我猜这是因为Visual Studio有“有趣”的入口点(即,不只是主要)。 如果您在VS中创建了正确的项目(例如控制台应用程序),它会为您创建正确的etrypoint。

您可以重构代码要么把你的所有“主”为切入点,或致电您的主要因为是从入口点(我建议重命名的main(),以避免混淆)

相关问题