4
#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?

5
  • The program works if I input an integer instead of sizeRow btw. Commented Aug 19, 2017 at 2:24
  • 1
    You would need support for C99 variable length arrays (VLA — or C11 and an implementation that doesn't define __STDC_NO_VLA__). AFAIK, MS Visual Studio still doesn't support them. GCC and Clang do. Commented Aug 19, 2017 at 2:26
  • Makes sense since I am using VS. Let me try CodeBlocks Commented Aug 19, 2017 at 2:26
  • @JonathanLeffler; Even if he will use GCC with c99 option on, he will get the same error message. Initializer list can't be used with VLA; 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.-§6.7.9-p(3) Commented Aug 19, 2017 at 4:07
  • 1
    @haccks: Actually, that's a different error message ("cannot initialize a VLA" is different from "dimension of an array must be a constant"). But you're right; once the OP gets past the current error, he will encounter the one you mention. Commented Aug 19, 2017 at 4:09

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So then how do I initialize my sum to 0?
4

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 (e.g. 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.

2 Comments

The last quote is wrong, GCC has for long defaulted to an extension standard that has supported VLAs.
gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/… gnu90 supports VLAs and the default standard used to be gnu90, not c90.
2

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;

1 Comment

@AnttiHaapala Thanks. It is added in vs15.
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

2 Comments

Welcome to Stack Overflow. Please read the About and How to Answer paqges before long. Why would a #define with no value be useful? How would you use them?
and moreover, the user will determine the size of my col and row, so define is useless?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.