jcrshankar
jcrshankar

Reputation: 83

passing array of structure as a function parameter

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;
}

I am facing error here, can any one please tell me what is the mistake i done here?

Function argument assignment between types "struct {...}*" and "struct {...}(*)[100]" is not allowed.

Expecting pointer to struct or union.

Upvotes: 1

Views: 4764

Answers (2)

Manuel Salvadores
Manuel Salvadores

Reputation: 16525

An array is intrinsically already a pointer to some space of memory where the length of the array has been allocated. Therefore you should simply do:

process_input_records(what_if_var);

without &

Upvotes: 3

Marc Mutz - mmutz
Marc Mutz - mmutz

Reputation: 25293

The error lies here:

process_input_records(&what_if_var);
                      ^

You're taking the address of an array, which is equivalent to what_if**, while the function takes only what_if*.

process_input_records(what_if_var);

Note that you probably want to pass the size of the array as a second parameter to process_input_records, so the function knows how many elements are in the array:

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

Upvotes: 2

Related Questions