【Dev-C++】中【函数原型】是否无法 定义【变长二维数组】(附【源代码】)
我用Dev-C++写这个程序时,发现编译出错,而使用学校的服务器则可以编译运行求大佬们指点一下
程序代码:
#include <stdio.h> void create_magic_square(int n, char magic_square[n][n]); //这个程序在 Dev-C++ 上编译错误 void print_magic_square(int n, char magic_square[n][n]); int main(void) { int n; printf("This is program creates a magic square of a specified size.\n"); printf("The size must be an odd number between 1 and 99.\n"); printf("Enter size of magic square: "); scanf("%d", &n); char a[n][n]; //变长数组无法直接 a[i][j] = {0}; create_magic_square(n, a); print_magic_square(n, a); return 0; } void create_magic_square(int n, char magic_square[n][n]) { int num = 1, i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) magic_square[i][j] = 0; //清零,为后面替换做准备; i = 0, j = n / 2; //开始的地方; while (num <= n * n) { //当 num < n * n 时, 循环下去; if (magic_square[i][j] == 0) { //当数组元素未被填入非零数字时替换; magic_square[i][j] = num++; i = (i - 1 + n) % n, j = (j + 1) % n; //当要用函数替换这个语句时,发现一个函数无法返回两个值,所以最后放弃用函数; } else { //当数组元素已被替换时下跑到前一个元素的下面,并继续执行斜向上替换直至结束; i = (i + 2) % n, j = (j - 1 + n) % n; magic_square[i][j] = num++; i = (i - 1 + n) % n, j = (j + 1) % n; } } } void print_magic_square (int n, char magic_square[n][n]) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) printf("%5d", magic_square[i][j]); putchar('\n'); } }