简体   繁体   English

如何正确传递数组作为函数参数?

[英]How to properly pass an array as a function argument?

When I try to send an array in to the function I get an error. 当我尝试将数组发送到函数时,我得到一个错误。

This is my minunit test program: 这是我的minunit测试程序:

#include "minunit.h"
#include "calc.h"
#include <stdio.h>

int tests_run = 0;

 static char * test_Repetitve() {
     mu_assert("error in test_Repetitive, Repetitive != 7", HistogramArray({1,2,3,4,5,6,7})== 7);
     return 0;
 }

 static char * all_tests() {
         mu_run_test(test_Repetitive);
     return 0;
 }

 int main(int argc, char **argv) {
     char *result = all_tests();
     if (result != 0) {
         printf("%s\n", result);
     }
     else {
         printf("ALL TESTS PASSED\n");
     }
     printf("Tests run: %d\n", tests_run);

     return result != 0;
 }

The line that I have problem with is 我遇到的问题是

mu_assert("error in test_Repetitive, Repetitive != 7", HistogramArray({1,2,3,4,5,6,7})== 7);

and it goes in to this function : 它进入这个功能:

    int HistogramArray(int one[])
{
    int arrchk[TWENTY+ONE] = { ZERO }, i, j,counter=0;//arrchk is an array that counts how many times the number appears.
    for (i = ZERO; i<N; i++)
        arrchk[one[i]]++;
    for (i = ZERO; i<TWENTY+ONE; i++)
    {
        if (arrchk[i] != ZERO) 
                        {
                         printf("the number is %d ", i);//printing the histogram.
                          counter++;
                        }
        for (j = ZERO; j<arrchk[i]; j++)
        {
            printf("*");

        }
        if (arrchk[i] != ZERO)printf("\n"); 
    }
return counter;

I basically need to check if the counter is 7 in the Histogram Function, any suggestions? 我基本上需要检查直方图函数中的计数器是否为7,有什么建议吗?

The problem is with the syntax HistogramArray({1,2,3,4,5,6,7}) , here {1,2,3,4,5,6,7} is not an array on it's own, it's a brace-enlosed list of initializers. 问题在于语法HistogramArray({1,2,3,4,5,6,7}) ,这里{1,2,3,4,5,6,7}不是它自己的数组,它是一个大括号的初始化列表。 The HistogramArray() function expects an array as argument. HistogramArray()函数需要一个数组作为参数。

You can however, use it with a syntax of compound literal 但是,您可以使用复合文字的语法

  HistogramArray((int []){1,2,3,4,5,6,7})

to use it like an array. 像数组一样使用它。

Quoting C11 , chapter §6.5.2.5, 引用C11 ,章节§6.5.2.5,

A postfix expression that consists of a parenthesized type name followed by a braceenclosed list of initializers is a compound literal. 后缀表达式由带括号的类型名称后跟一个大括号的初始值设定项列表组成,是一个复合文字。 It provides an unnamed object whose value is given by the initializer list. 它提供了一个未命名的对象,其值由初始化列表给出。

and

If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in 6.7.9, and the type of the compound literal is that of the completed array type. 如果类型名称指定了未知大小的数组,则大小由6.7.9中指定的初始化程序列表确定,复合文字的类型是已完成数组类型的类型。 [...] [...]

So, this provides with you with an unnamed array which is initialized with the elements in the brace enclosed list. 因此,这为您提供了一个未命名的数组,该数组使用括号括起的列表中的元素进行初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM