传递可变大小的多维数组没有VLA [英] Passing a multidimensional array of variable size without VLA

查看:102
本文介绍了传递可变大小的多维数组没有VLA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是一个跟进<一个href=\"http://stackoverflow.com/questions/34559211/passing-a-multidimensional-array-of-variable-size\">to这个问题。)

我想了解什么最佳实践(或者任何其他的做法)是传递一个多维数组给函数在C.当然,这取决于应用程序,所以让我们考虑写一个函数打印二维数组的可变大小。特别是,我感兴趣的是怎么一会写在下面的code函数 printArry(_____)

在上面提到的问题的答案使用可变长度阵列(VLA)C. 这些都是不是在C98标准present ,并在C11标准可选的(他们也似乎没有编译C ++ 11任)。

一个人怎么会回答不使用VLA功能这个问题?传递一个二维数组似乎是一个相当基本的任务,所以我不能想象没有一个合理的方法了这一点。

 无效printArry(_____){
    / *发生的事情吗? * /
}诠释主要(无效){    INT A1 = 5;
    INT B1 = 6;
    INT A2 = 7;
    INT B2 = 8;    INT arry1 [A1] [B1];
    INT arry2 [A2] [B2];    / *在数组中设定值* /    printArry(arry1,A1,B1);
    printArry(arry2,A2,B2);
    返回0;
}


解决方案

现在的问题是目前问题的:


  

在上面提到的问题的答案使用C的可变长度数组(VLA)的特性,这些是不是在C98标准present并且在C11标准可选


之前C99(ISO / IEC 9899:1990)的C标准是C89(ANSI)和C90(ISO) - 功能相同的标准。有C ++ 98(ISO / IEC 14882:1998),但是这是一个完全不同的语言


  INT主要(无效)
{
    INT A1 = 5;
    INT B1 = 6;
    INT A2 = 7;
    INT B2 = 8;    INT arry1 [A1] [B1];
    INT arry2 [A2] [B2];


本示例code使用沃拉斯;它不会在严格的C90编译​​规则


如果你被限制到pre-1999 C标准,那么你被限制使用与此类似code。很明显,你可以通过读取一个文件或其他任何需要你的想象数据初始化数组;使用直接初始化是可能的,因为数组不再沃拉斯。

 的#include&LT;&stdio.h中GT;无效printArry(INT D1,D2 INT,INT *数据);INT主要(无效)
{
    枚举{A1 = 5,B1 = 6,A2 = 7,B2 = 8};
    INT arry1 [A1] [B1] =
    {
        {9,8,7,6,5,4},
        {2,3,4,3,2,1},
        {-9,-8,-7,-6,-5,-4},
        {39,38,37,36,35,34},
        {99,98,97,96,95,94},
    };
    INT arry2 [A2] [B2] =
    {
        {198,158,165,136,198,127,119,103,},
        {146,123,123,108,168,142,119,115,},
        {160,141,168,193,152,152,147,137,},
        {144,132,187,156,188,191,196,144,},
        {197,164,108,119,196,171,185,133,},
        {107,133,184,191,166,105,145,175,},
        {199,115,197,160,114,173,176,184,},
    };    printArry(A1,B1,与放大器; arry1 [0] [0]);
    printArry(A2,B2,&放大器; arry2 [0] [0]);    返回0;
}无效printArry(INT D1,D2 INT,INT *数据)
{
    INT I,J;
    对于(i = 0; I&LT; D​​1,我++)
    {
        为(J = 0; J&LT; D​​2; J ++)
            的printf(%4D,数据[我* D2 + J]);
        的putchar('\\ n');
    }
}

printArry()功能,从本质上讲,做指数(下标)的计算,你想编译器为你做 - 但它不能因为它是不是能够处理C99沃拉斯。

数据的第一阵列是手工制作,让问题被发现(如使用 D 1 而不是 D 2 在标计算)。数据的第二阵列是100和199之间简单地56随机值

示例输出:

  9 8 7 6 5 4
    2 3 4 3 2 1
   -9 -8 -7 -6 -5 -4
   39 38 37 36 35 34
   99 98 97 96 95 94
  198 158 165 136 198 127 119 103
  146 123 123 108 168 142 119 115
  160 141 168 193 152 152 147 137
  144 132 187 156 188 191 196 144
  197 164 108 119 196 171 185 133
  107 133 184 191 166 105 145 175
  199 115 197 160 114 173 176 184

(This is a follow up to this question.)

I'm trying to understand what "best practice" (or really any practice) is for passing a multidimensional array to a function in C. Certainly this depends on the application, so lets consider writing a function to print a 2d array of variable size. In particular, I'm interested in how one would write the function printArry(_____) in the following code.

The answer in the question referenced above uses variable length array (VLA) features of C. These are not present in the C98 standard and are optional in the C11 standard (they also don't seem to compile in C++11 either).

How would one answer this question without using VLA functionality? Passing a two dimensional array seems like a fairly basic task, so I can't imagine there isn't a reasonable approach to this.

void printArry(_____){
    /* what goes here? */
}

int main(void){

    int a1=5;
    int b1=6;
    int a2=7;
    int b2=8;

    int arry1[a1][b1];
    int arry2[a2][b2];

    /* set values in arrays */

    printArry(arry1, a1, b1);
    printArry(arry2, a2, b2);
    return 0;
}

解决方案

The question is currently problematic:

The answer in the question referenced above uses variable length array (VLA) features of C. These are not present in the C98 standard and are optional in the C11 standard.

The C standard prior to C99 (ISO/IEC 9899:1990) was C89 (ANSI) or C90 (ISO) — functionally the same standard. There was C++98 (ISO/IEC 14882:1998), but that's a wholly different language.

int main(void)
{
    int a1=5;
    int b1=6;
    int a2=7;
    int b2=8;

    int arry1[a1][b1];
    int arry2[a2][b2];

This sample code is using VLAs; it will not compile under strict C90 rules.


If you are constrained to the pre-1999 C standard, then you are constrained to using code similar to this. Clearly, you can initialize the arrays by reading data from a file or anything else that takes your fancy; using direct initializers is possible because the arrays are no longer VLAs.

#include <stdio.h>

void printArry(int d1, int d2, int *data);

int main(void)
{
    enum { a1 = 5, b1 = 6, a2 = 7, b2 = 8 };
    int arry1[a1][b1] =
    {
        {  9,  8,  7,  6,  5,  4 },
        {  2,  3,  4,  3,  2,  1 },
        { -9, -8, -7, -6, -5, -4 },
        { 39, 38, 37, 36, 35, 34 },
        { 99, 98, 97, 96, 95, 94 },
    };
    int arry2[a2][b2] =
    {
        { 198, 158, 165, 136, 198, 127, 119, 103, },
        { 146, 123, 123, 108, 168, 142, 119, 115, },
        { 160, 141, 168, 193, 152, 152, 147, 137, },
        { 144, 132, 187, 156, 188, 191, 196, 144, },
        { 197, 164, 108, 119, 196, 171, 185, 133, },
        { 107, 133, 184, 191, 166, 105, 145, 175, },
        { 199, 115, 197, 160, 114, 173, 176, 184, },
    };

    printArry(a1, b1, &arry1[0][0]);
    printArry(a2, b2, &arry2[0][0]);

    return 0;
}

void printArry(int d1, int d2, int *data)
{
    int i, j;
    for (i = 0; i < d1; i++)
    {
        for (j = 0; j < d2; j++)
            printf(" %4d", data[i * d2 + j]);
        putchar('\n');
    }
}

The printArry() function is, essentially, doing the index (subscript) calculation that you would like the compiler to do for you — but it can't because it is not able to handle C99 VLAs.

The first array of data is hand-crafted to allow problems to be spotted (such as using d1 instead of d2 in the subscript calculation). The second array of data is simply 56 random values between 100 and 199.

Sample output:

    9    8    7    6    5    4
    2    3    4    3    2    1
   -9   -8   -7   -6   -5   -4
   39   38   37   36   35   34
   99   98   97   96   95   94
  198  158  165  136  198  127  119  103
  146  123  123  108  168  142  119  115
  160  141  168  193  152  152  147  137
  144  132  187  156  188  191  196  144
  197  164  108  119  196  171  185  133
  107  133  184  191  166  105  145  175
  199  115  197  160  114  173  176  184

这篇关于传递可变大小的多维数组没有VLA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆