注:固定(底部说明)
出于某种原因,以下代码:
(*p_to_array)[m_p->number_of_match_positions] = (*p_to_temp_array)[k];
其中类型是:
match_pos_t (*p_to_array)[];
match_pos_t (*p_to_temp_array)[];
int number_of_match_positions;
int k;
顺便说一句:match_pos_t 是一个结构:
typedef struct match_pos
{
char* string;
long match_position;
}match_pos_t;
在 '(' error' 之前导致 'syntax error'
如果将此代码替换为其他代码,则不会发生此错误。
有人可以告诉我为什么这会导致语法错误,以及我应该如何解决这个问题?
完整的相关代码:
typedef struct match_pos
{
char* string;
long match_position;
}match_pos_t;
typedef struct match_positions
{
int number_of_match_positions;
match_pos_t (*match_positions)[];
}match_positions_t;
typedef struct search_terms
{
int number_of_search_terms;
char* search_terms[];
}search_terms_t;
int BMH_string_search(char* search_string, char* file_string, match_positions_t* match_positions)
{
return 0;
}
int determine_match_pos(search_terms_t** s_terms, char* file, match_positions_t* m_p)
{
int i,j,k;
match_positions_t* temp_m_p;
i=0;
/* s_terms is a null terminated data structure */
while((*s_terms+i) != NULL)
{
for(j=0; j<(*s_terms+i)->number_of_search_terms; j++)
{
/* search for the string positions */
BMH_string_search((*s_terms+i)->search_terms[j], file, temp_m_p);
/* load out search positions into the return array */
if(temp_m_p->number_of_match_positions != 0)
{
int total_m_ps = m_p->number_of_match_positions + temp_m_p->number_of_match_positions;
m_p->match_positions = (match_pos_t (*)[])realloc(m_p->match_positions, sizeof(match_pos_t)*total_m_ps);
k = 0;
for( ; m_p->number_of_match_positions<total_m_ps; m_p->number_of_match_positions++)
{
(*(m_p->match_positions))[m_p->number_of_match_positions] = (*(temp_m_p->match_positions))[k];
k++;
}
}
free(temp_m_p);
}
i++;
}
return 0;
}
看来我已经相当愚蠢了。围绕被引用的值的一组额外的括号可以解决问题(问题代码已通过修复更新):原始:
(m_p->*match_positions)[m_p->number_of_match_positions] = (temp_m_p->*match_positions)[k];
固定的:
(*(m_p->match_positions))[m_p->number_of_match_positions] = (*(temp_m_p->match_positions))[k];
但是,如果有人解释为什么第一个是不正确的,而不是第二个,那么很高兴听到,因为我认为
object->*object2
是一样的
*(object->object2)
这是正确的还是我在这里遗漏了一些 c 定义?