In visual studio 2022 I can't compile this little program and on Clion, visual studio code and others it compiles.
I don't know if it could be a problem of the clang compiler or some configuration of visual studio (but stills on default).
#include <stdio.h>
int main() {
//Variable declarations
int size;
printf("INPUT\n");
printf("SIZE (2-3)?\n");
scanf("%d", &size);
int t[size][size]; //This is marked as wrong (only in visual studio 2022 not other IDE's)
int i = 0;
int j = 0;
//matrix read
for (i = 0; i < size; i ) {
for (j = 0; j < size; j ) {
printf("POSITION(%d, %d)?\n", i, j);
scanf("%d", &t[i][j]);
}
}
int calc = 0;
//calc matrix
for (i = 0; i < size; i ) {
for (j = 0; j < size; j ) {
calc = calc t[i][j];
}
}
printf("suma: %d", calc);
return 0;
}
Thanks for reading.
I've tested the code on other IDE's and compiles without problems.
CodePudding user response:
Instead of a Variable Length Array, a pointer to pointer, int **t
, could be used.
Allocate the pointers and then for each pointer, allocate the elements.
#include <stdio.h>
#include <stdlib.h>
int main() {
//Variable declarations
int size;
printf("INPUT\n");
printf("SIZE (2-3)?\n");
if ( 1 != scanf("%d", &size)) {
fprintf ( stderr, "Could not scan an integer\n");
return 1;
}
int **t = NULL; // pointer to pointer
if ( NULL == ( t = malloc ( size * sizeof *t))) {
fprintf ( stderr, "Could not allocate pointers\n");
return 1;
}
int each = 0;
for ( each = 0; each < size; each) {
if ( NULL == ( t[each] = malloc ( size * sizeof **t))) {
fprintf ( stderr, "Could not allocate elements\n");
return 1;
}
}
int i = 0;
int j = 0;
//matrix read
for (i = 0; i < size; i ) {
for (j = 0; j < size; j ) {
printf("POSITION(%d, %d)?\n", i, j);
if ( 1 != scanf("%d", &t[i][j])) {
fprintf ( stderr, "Could not scan an integer\n");
return 1;
}
}
}
int calc = 0;
//calc matrix
for (i = 0; i < size; i ) {
for (j = 0; j < size; j ) {
calc = calc t[i][j];
}
}
printf("suma: %d\n", calc);
for (i = 0; i < size; i ) {
free ( t[i]);
}
free ( t);
return 0;
}