简体   繁体   English

使用变量声明数组的大小

[英]Declaring Size of Array Using Variable

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

#pragma warning (disable : 4996)

void main() {

    int matrix[30][50];

    int sizeRow, sizeCol;

    printf("Number of Rows in your table    : ");
    scanf("%d", &sizeRow);

    printf("Number of Columns in your table : ");
    scanf("%d", &sizeCol);

    int sum[sizeRow] = { 0 };

    for (int row = 0; row < sizeRow; row++){
        for (int col = 0; col < sizeCol; col++){
            printf("Input element [%d][%d] : ", row, col);
            scanf("%d", &matrix[row][col]);
            sum[row] += matrix[row][col];
        }
    }

    printf("Total of each row:\n");
    for (int row = 0; row < sizeRow; row++){
        printf("ROW[%d] SUM :\t%d\n", row, sum[row]);
    }

    system("pause");
}

I am getting error in the int sum[sizeRow] = { 0 }; 我在int sum[sizeRow] = { 0 };得到错误int sum[sizeRow] = { 0 }; where it says that my array should be a constant but the user in my case should determine the array size. 它说我的数组应该是一个常量,但在我的情况下,用户应该确定数组大小。 Any way I can fix this? 我能解决这个问题吗?

MSVC doesn't support variable length arrays. MSVC不支持可变长度数组。 You'll need to allocate memory with calloc . 你需要用calloc分配内存。 Unlike malloc , calloc initializes all bytes to 0: malloc不同, calloc将所有字节初始化为0:

int *sum = calloc(sizeRow, sizeof(int));

Don't forget to free the memory afterward. 不要忘记随后free记忆。

I have just noticed that you are using MSVC. 我刚刚注意到你正在使用MSVC。 Nowadays, it is possible to use VLAs with it. 如今,可以使用VLA。 Since Visual Studio 2015, it [almost] fully implements C99, but still treats all C99 features as language extensions (eg disabling language extensions disables C99 support as well). 从Visual Studio 2015开始,它[几乎]完全实现了C99,但仍然将所有C99功能视为语言扩展(例如,禁用语言扩展也禁用C99支持)。 As a result, you either use prior version or disabled some extensions. 因此,您要么使用先前版本,要么禁用某些扩展。 Moreover, on the next step, you most likely encounter with the message that variable-sized object may not be initialized. 此外,在下一步中,您很可能会遇到可能无法初始化可变大小对象的消息

The following example demonstrates how to use C99 Variable Length Arrays (VLAs) in a firstprivate directive ( Section 2.7.2.2 on page 26). 以下示例演示如何在firstprivate指令中使用C99可变长度数组(VLA)( 26页的第2.7.2.2节 )。 Source: MS Developer Network 资料来源: MS Developer Network

void f(int m, int C[m][m])  
{  
    double v1[m];  
    ...  
    #pragma omp parallel firstprivate(C, v1)  
    ...  
}  

Assuming your "problem" is that the compiler objects, if you're using GCC or Clang, try adding the flag -std=c99 or -std=c11 to your command line. 假设您的“问题”是编译器对象,如果您正在使用GCC或Clang,请尝试将标志-std=c99-std=c11到命令行。 GCC defaults to an older version of the C language that doesn't have this functionality. GCC默认为没有此功能的旧版C语言。

You don't need malloc unless you intend to return the array. 除非您打算返回数组,否则不需要malloc。 Always use the simplest thing that will work. 始终使用最简单的方法。

int sum[sizeRow] declares a variable length array which is introduced in C99 and older versions of MSVC does not support VLAs. int sum[sizeRow]声明一个在C99中引入的可变长度数组,旧版本的MSVC不支持VLA。
Also note that one of the restriction on VLA is that it can't be initialized with initializer list and therefore the line 另请注意,对VLA的限制之一是它无法使用初始化程序列表进行初始化,因此无法使用行进行初始化

int sum[sizeRow] = { 0 };

will raise an error message even if you compile with a compiler that does support VLA. 即使您使用支持VLA的编译器进行编译,也会引发错误消息。

§6.7.9-p(3): §6.7.9-P(3):

The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type. 要初始化的实体的类型应为未知大小的数组或不是可变长度数组类型的完整对象类型。

Either use memset 要么使用memset

int sum[sizeRow];  
memset(sum, 0, sizeof(sum));  

or a for loop to initialize it 或者一个for循环来初始化它

for(int i = 0; i < sizeRow; i++)
    sum[i] = 0;

You should include a #define directive to set the array rows and columns then make the user to input them, like this: 您应该包含#define指令来设置数组行和列,然后让用户输入它们,如下所示:

#define ROWCOL
#define COLUMNCOL

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

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