“warning: 'struct matrix' declared inside

2019-02-27 03:38发布

问题:

I've been pouring over this issue trying to figure out what is causing these errors, but so far I've come up with nothing. I have this function:

    struct token scanToken(struct matrix refTable){
        struct token send;
        int counter = 0;
        int currState = refTable.start;
        while (1) {
                printf("%d ", currState);
                char c = getchar();
                send.buffer[counter] = c;
                int class = classifyChar(c);
                char result = refTable.grid[class][currState].action;
                currState = refTable.grid[class][currState].nextState;
                if (currState = 99){
                        findEnd();
                        send.recrej = 'd';
                        return send;
                }
                else if (currState = refTable.accept){
                        if (c == EOF){
                                send.isEnd = 't';
                        }
                        else{
                                send.isEnd = 'f';
                                send.recrej = 'a';
                        }
                }
                ++counter;
        }
        return send;
}

It is matched with the following header file:

  struct token {
        char buffer[512];
        char recrej;
        char isEnd;
};


void findEnd(void);

struct token scanToken(struct matrix refTable);

int classifyChar(char c);

This code is currently used in this snippet in my main function:

 struct matrix table;
        table = buildMatrix(argv[1]);
        char c;
        int class;
        struct token found;
        found = scanToken(table);
        while(found.isEnd != 't'){
                if (found.recrej == 'a'){
                        printf("recognized '%s'\n", found.buffer);
                }
                else {
                        printf("rejected\n");
                }
                found = scanToken(table);
        }

the matrix struct is prototyped in another header file, which is included in the scanner.c file (pictured first), and the tokenize.c file (pictured last). However, this produces the following warnings and errors.

In file included from scanner.c:10:0:
scanner.h:16:31: warning: 'struct matrix' declared inside parameter list [enabled by default]
 struct token scanToken(struct matrix refTable);
                               ^
scanner.h:16:31: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
scanner.c:60:14: error: conflicting types for 'scanToken'
 struct token scanToken(struct matrix refTable){
              ^
In file included from scanner.c:10:0:
scanner.h:16:14: note: previous declaration of 'scanToken' was here
 struct token scanToken(struct matrix refTable);

I've been searching for quite some time, and have tried rewriting things a number of ways, with the same result all around. Any help would be greatly appreciated. Thank you.

回答1:

You have to declare struct matrix outside a function prototype, like the error message implies.

You have in your scanner.h header:

struct token scanToken(struct matrix refTable);

Since there's no prior declaration of struct matrix in that header, or a header read before it is read, the struct matrix is a new distinct type. It's also incomplete, so you really need to use a pointer to it.

You can fix it as simply as:

struct matrix;
struct token scanToken(struct matrix *refTable);

To be able to pass the struct matrix by value instead of by pointer, you need a full definition of the structure, but pointers can be passed to incomplete types.

Or include the header that defines the struct matrix fully in the scanner.h header.

Note that you should protect your headers with multiple inclusion guards:

#ifndef SCANNER_H_INCLUDED
#define SCANNER_H_INCLUDED

…current contents…

#endif // SCANNER_H_INCLUDED

You might well add #include "otherheader.h" in that one — the other header that defines struct matrix in full.