将结构数组作为函数参数传递

时间:2011-04-27 07:53:29

标签: c

typedef struct What_if
{
    char   price                 [2];
} what_if ;

what_if  what_if_var[100];

int format_input_records();

int process_input_records(what_if *what_if_var);

int format_input_records()
{
    if (infile != NULL )
    {
        char mem_buf [500];

        while ( fgets ( mem_buf, sizeof mem_buf, infile ) != NULL ) 
        {
            item = strtok(mem_buf,delims);     
            strcpy(what_if_var[line_count].trans_Indicator,item) ;
            printf("\ntrans_Indicator     ==== : : %s",what_if_var[line_count].price);
            process_input_records(&what_if_var);
            line_count=line_count+1;
        }
    }
}

int process_input_records(what_if *what_if_var)
{
    printf("\nfund_price process_input_records    ==== : : %s",what_if_var[line_count]->price);
    return 0;
}

我在这里遇到错误,任何人都可以告诉我在这里做的错误是什么?

  

不允许在类型“struct {...}*”和“struct {...}(*)[100]”之间进行函数参数分配。

     

期望指向struct或union的指针。

2 个答案:

答案 0 :(得分:2)

数组本质上已经是指向已分配数组长度的内存空间的指针。因此,您应该这样做:

process_input_records(what_if_var);

没有&

答案 1 :(得分:2)

错误在于:

process_input_records(&what_if_var);
                      ^

您正在获取数组的地址,该地址相当于what_if**,而该函数仅使用what_if*

process_input_records(what_if_var);

请注意,您可能希望将数组的大小作为第二个参数传递给process_input_records,因此该函数知道数组中有多少元素:

process_input_records( what_if_var, sizeof  what_if_var / sizeof *what_if_var );