【发布时间】:2021-02-21 11:02:25
【问题描述】:
我有一个在主函数中初始化的动态字符串数组,但我不确定如何正确使用指针并将我的数组发送给函数。在 function_w 中,我想使用数组,然后将其发送回主函数,然后在其他函数中使用。 这是我的代码:
char function_w(char **array,FILE **fr)
{
int i = 0, strcount = 0;
int buf_length = 50;
char buf [buf_length],p = NULL;
fseek(*fr, 0, SEEK_SET);
while (fgets(buf, buf_length, *fr) != NULL)
{
array = (char **)realloc(array, (strcount + 1) * sizeof(char *));
array[strcount++] = strdup(buf);
}
return p;
}
int main(void)
{
char **array = NULL;
FILE*fr = NULL; //file is opened in other function, before calling w
...
if (fr != NULL) **array = function_w(array,&fr);
else printf("Error");
return 0;
}
每次我使用 function_w 时都没有输出。你能帮帮我吗?
【问题讨论】:
-
我的c技能有点过时但不应该是
*fr而不是&fr -
@SimonDanninger 不,因为您通常不会触及结构的内容(?)
FILE。它应该是fr,我认为应该相应地更改参数。
标签: arrays c file pointers dynamic-arrays