简体   繁体   中英

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 }; 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. You'll need to allocate memory with calloc . Unlike malloc , calloc initializes all bytes to 0:

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

Don't forget to free the memory afterward.

I have just noticed that you are using MSVC. Nowadays, it is possible to use VLAs with it. 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). 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). Source: 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 defaults to an older version of the C language that doesn't have this functionality.

You don't need malloc unless you intend to return the array. 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.
Also note that one of the restriction on VLA is that it can't be initialized with initializer list and therefore the line

int sum[sizeRow] = { 0 };

will raise an error message even if you compile with a compiler that does support VLA.

§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

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

or a for loop to initialize it

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 ROWCOL
#define COLUMNCOL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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