将结构数组作为函数参数传递
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的指针。
数组本质上已经是指向已分配数组长度的某个内存空间的指针。 因此你应该简单地做:
process_input_records(what_if_var);
没有&
错误在于:
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 );