[英]Referencing the values in pointers to arrays (c)
注意:固定(在底部的十進制)
由於某種原因,以下代碼:
(*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定義?
我以為
*(object->object2)
object->*object2
與*(object->object2)
不,在C中.
->
運算符期望將標識符作為其正確的操作數。 .*
和->*
運算符在C中不存在,您必須手動拼寫*(structure.member)
或*(structure_ptr->member)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.