在 Visual Studio 2022 中,我無法編譯這個小程式,而在 Clion、Visual Studio 代碼和它編譯的其他程式上。

我不知道這是否可能是 clang 編譯器或 Visual Studio 的某些配置的問題(但默認情況下仍然存在)。
#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;
}
謝謝閱讀。
我已經在其他 IDE 上測驗了代碼并且編譯沒有問題。
uj5u.com熱心網友回復:
int **t可以使用指向指標 的指標來代替可變長度陣列。
分配指標,然后為每個指標分配元素。
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520282.html
標籤:C视觉工作室编译器错误
