繁体   English   中英

从linux到Windows 7上的Visual Studio 2010的C编程问题

[英]c programming issues going from linux to visual studio 2010 on windows 7

基本上,我上过一门C编程课程,在该课程中,我们使用Linux机器编写和编译我们的代码。

下面的程序是我们第一个任务的一部分,我在上课时编译并没有问题地在Linux上运行了该程序,但是将其带回家后,我一生都无法在Visual Studio 2010 Ultimate中对其进行编译,或者使用MinGW编译器使Eclipse失效。

在两个操作系统之间切换会导致我的代码失败吗,这是否存在一些典型的问题? 还是我(作为我的菜鸟)编写了一些丑陋的代码,而这些代码与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;
}

问题在于,在编译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中这是不允许的,并且MSVC在C模式下进行编译时会抱怨(如果在C ++中编译则不会,因为C ++支持这种情况)。

要解决该问题,只需在块开始之后移动声明即可:

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);

    //...

有时,这将要求您重新初始化初始化,而其他则不需要。

要解决使用可变长度数组的问题,需要做更多的工作。 在这种情况下,我认为您可以通过将userArray声明为int*并使用malloc()为其分配存储来实现:

int* userArray;

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

其他一些事情:

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

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

您可以重构代码以将所有“ main”放入入口点或从入口点直接调用main(我建议重命名main()以避免混淆)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM