gpt4 book ai didi

c - 引用数组指针中的值 (c)

转载 作者:行者123 更新时间:2023-11-30 15:48:51 24 4
gpt4 key购买 nike

注意:已修​​复(底部说明)

出于某种原因,以下代码:

(*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;

在“(”错误之前导致“语法错误”
如果将此代码替换为其他代码,则不会出现此错误。

有人可以告诉我为什么这会导致语法错误,以及我应该如何解决这个问题吗?

完整相关代码:

 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 定义?

最佳答案

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

不,在 C 语言中,.-> 运算符期望标识符作为其右操作数。 C 中不存在 .*->* 运算符,您必须拼写出 *(structural.member)*(structure_ptr->member) 手动。

关于c - 引用数组指针中的值 (c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16622395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com