2014-09-23 84 views
2

我正在用指针挣扎一下。我试图在函数之间传递信息,但得到以下错误:“请求成员”得分“的东西不是一个结构或联盟”,和“索引”而不是得分相同的错误(和所有其他成员我这样做并没有在这里列出)。使用struct在函数之间传递数组信息

这里是我的结构:

typedef struct line_t { 
    char* buf; 
    int lineLength; 
    int wordCount; 
    int index; 
    double score; 
} line_t; 

这是我的调用函数在main(与报关):

line_t bestmatch[TOP_SCORING_MAX]; 
func3(&line, &bestmatch[TOP_SCORING_MAX]); 

这里是我的功能:

line_t 
func3(line_t line, line_t *bestmatchPtr[]) { 
    int i; 

    for (i=0; i< TOP_SCORING_MAX; i++) { 

     if (line.score != 0) { 

      if (i == 0) { 

       bestmatchPtr[0].score = line.score; 
       bestmatchPtr[0].index = line.index; 

       /*more code here: rest of func, closing of }, etc*/ 

return bestmatchPtr; 
} 

本质,我需要传递有关函数间bestmatch的信息,同时保持它的顺序(我的函数试图订购信息只保留一定数量的数据)。我想知道如何解决这个错误? 如果我遗失了一些信息,请告诉我。

+0

一些编译器不能通过结构或归还。尝试使retutn值为line_t *,同上参数行。请记住使用 - >运算符而不是要求元素。 – rhubarbdog 2014-09-23 10:33:50

回答

3

func3签名应该是:

line_t *func3(line_t line, line_t *bestmatchPtr) 
/* Changed: second argument and return type */ 

还要注意的是:

  • 您的数组的最后一个元素是
  • line参数按值传递bestmatch[TOP_SCORING_MAX - 1](NOT bestmatch[TOP_SCORING_MAX])(&line是一个指针)

因此,这是错误的:

func3(&line, &bestmatch[TOP_SCORING_MAX]); 

函数调用应该是:

func3(line, bestmatch); 
+1

同时传递'&line'的类型是'line_t *'而不是'line_t',或者不是? – 0xfee1dead 2014-09-23 10:23:44

+2

即函数调用应该是'func3(line,bestmatch);' – juanchopanza 2014-09-23 10:24:31