2

I'm struggling a bit with pointers. I'm trying to pass information between functions, but am getting the following error: "request for member ‘score’ in something not a structure or union", and the same error with 'index' instead of score (and for all other members I'm doing this for that are not listed here).

Here is my struct:

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

This is my call to the function in main (with declaration):

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

Here is my function:

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;
}

Essentially, I need to pass information about bestmatch between functions, while keeping it ordered (my function is attempting to order information and retain only a set amount of data). I was wondering how to fix this error? Let me know if I'm missing some information.

1
  • Some compilers can't pass structures or return them. Try making the retutn value be line_t *, ditto for parameter line. Remember to derefence elements with the -> operator rather than . Commented Sep 23, 2014 at 10:33

1 Answer 1

3

func3 signature should be:

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

Also note that:

  • the last element of your array is bestmatch[TOP_SCORING_MAX - 1] (NOT bestmatch[TOP_SCORING_MAX])
  • the line argument is passed by value (&line is a pointer)

So this is wrong:

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

the function call should be:

func3(line, bestmatch);
Sign up to request clarification or add additional context in comments.

2 Comments

Also passing &line will be of type line_t* instead line_t, or not?
i.e. the function call should be func3(line, bestmatch);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.