是否可以打字一个不确定大小的多维阵列?[C]



我当前正在尝试解决特定任务。为了完成这项任务,我尝试练习到目前为止我学到的所有技能。由于我经常处理一个多维阵列,所以我想像往常一样打字。

这次阵列必须用大小的程序进行编辑。

typedef char grid[][];

这是我在特殊标题文件中尝试的。我遇到错误

error: array type has incomplete element type

如果有必要查看更多详细信息,请发布完整的代码:

main.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "functions.h"
#include "tester.h"
#include "magic.h"
int main(int argc, char **argv)
{
    int size = getSize();
    grid pascal[size][size];
    drawGrid(pascal, size);
    return EXIT_SUCCESS;
}

functions.c

#include <stdlib.h>
#include <stdio.h>
#include "magic.h"
#include <math.h>
/*  C array: Pascal triangle exercise
*  By using two-dimensional array, write C program to display
*  a table that represents a Pascal triangle of any size.
*
*  In Pascal triangle, the first and the second rows are set to 1.
*  Each element of the triangle (from the third row downward) is the sum
*  of the element directly above it and the element to the left of the
*  element directly above it. See the example Pascal triangle(size=5) below:
*
*
*  1
*  1    1
*  1    2   1
*  1    3   3   1
*  1    4   6   4   1
*
*/
int getSize(void)
{
    int size;
    printf("Please enter the size of the Pascal Triangle:");
    scanf("%d",&size);
    printf("n");
    size = (int) sqrt(size);
    printf("Pascal Triangle will be %d big", (size*size));
    return size;
}
void createVoid(grid pascal, int size)
{
    int i = 0;
    int j = 0;
    while(i < size){
        while (j < size){
            if (j > i){
                pascal[i][j] = ' ';
            }
            j++;
       }
       i++;
    }
}
void createNumbers(grid pascal, int size)
{
    int i = 0;
    int j = 0;
    while(i < size){
        while (j < size){
            if (j <= i){
                if (i == 0){
                    pascal[i][j] = '1';
                }else if (i == j){
                    pascal[i][j] = '1'
                }else{
                    pascal[i][j] = pascal[i][j-1] - VALUE_ZERO + pascal[i-1][j];
                }
            }
            j++;
        }
        i++;
    }
}
void printGrid(grid pascal)
{
    int i = 0;
    int j = 0;
    while (i < size){
        while (j < size){
            printf ("%3c", pascal[i][j]);
            j++;
        }
        i++;
    }
}
void drawGrid(grid pascal, int size)
{
    createVoid(pascal, size);
    createNumbers(pascal, size);
    printGrid(pascal);
}

functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "magic.h"
int getSize(void);
void drawGrid(grid pascal, int size);
void createVoid(grid pascal, int size);
void createNumbers(grid pascal, int size);
void printGrid(grid pascal, int size);
#endif // FUNCTIONS_H

Magic.h

#ifndef MAGIC_H
#define MAGIC_H
#include <stdlib.h>
#define VALUE_ZERO '0'
typedef char grid[][];
#endif // MAGIC_H

typedef声明 char grid[][]是非法的,因为您不能省略最大的indice。通常,您只能省略最左侧的一个。

C99标准引入了一种使用未知大小的多维阵列声明和定义函数的方法(称为VLAS 1 )。声明此类功能的适当形式是:

void drawGrid(int, char pascal[*][*]);

,或者如果您希望保留参数的名称:

void drawGrid(int size, char pascal[size][size]);

后者(只有后者)也可以用于定义。

请注意,typedef声明不能在文件范围处应用于VLA(即在任何函数之外)。参考C11(N1570草案),第6.7.8/2节类型定义:

如果typedef名称指定了一个可变修改的类型,则应 具有块范围。


1)当然,vlas也可能是一维的,例如int n = 100; int a[n] = {0};

最新更新