2013-05-18 50 views
0

注:固定(在底部能解密)引用在指針的值,以陣列(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; 

BTW:match_pos_t是一個結構:

typedef struct match_pos 
{ 
    char* string; 
    long match_position; 
}match_pos_t; 

會導致'語法錯誤'('錯誤'
如果此代碼被其他代碼替換,則不會發生此錯誤。

有人可以告訴我爲什麼這會導致語法錯誤,我該如何解決這個問題?

整個相關代碼:

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的定義,我錯過了在這裏?

+0

有很多可能的解釋。嘗試http://sscce.org獲取更好的答案的指導。 –

+0

請顯示完整的代碼。 – Deepu

+0

如何解決第10行的錯誤:'錯誤:數組類型具有不完整的元素類型'(由GCC 4.7.1給出)? –

回答

1

I thought that object->*object2 was the same as *(object->object2)

不,在C,在.->運營商期望的標識符作爲其右操作數。在C中不存在.*->*運算符,您必須手動輸出*(structure.member)*(structure_ptr->member)