1

I'm trying to solve Conway's Game of Life in C. I have written a .h file containing all my functions, yet I receive the following error within the header file: error: unknown type name "matrix"

This is the beginning of the header file, which contains my struct declaration and the 1st function:

#include<stdio.h>
#include<string.h>
#define MAX 1000
struct matrix{
    int Val, Next;
};
void intro_date(int nr_elem, matrix a[MAX][MAX]){
    int x,y;
    printf("Enter the line and the column of the element which you wish to read within the matrix: \n");
    while(nr_elem){
        scanf("%d%d",&x,&y);
        a[x][y].Val=1;
        --nr_elem;
    }
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
theSongbird
  • 229
  • 1
  • 3
  • 13
  • 3
    In C you can not ommit the `struct` keyword, `matrix a[MAX][MAX]` should be `struct matrix a[MAX][MAX]` (or use a `typedef`) – David Ranieri Nov 02 '16 at 17:42
  • Here is explained: http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions – germanfr Nov 02 '16 at 17:44
  • Or the definition should be `typedef struct matrix{ int Val, Next; } matrix;`. – JohnB Nov 02 '16 at 17:44
  • 2
    You should not put function implementations in C header files, only their prototypes. Including the implementation as well tends to produce duplicate definitions, which are not allowed. – John Bollinger Nov 02 '16 at 17:45

4 Answers4

7

You defined a structure called struct matrix. This is not the same as matrix, as struct definitions must be preceeded by the struct keyword.

Change your function definition to:

void intro_date(int nr_elem, struct matrix a[MAX][MAX])

Also, you should not put code into a header file. Only type definitions and declarations belong there. If more than one source file were to include this header, the object file created for each will contain a copy of the function intro_date(). Upon attempting to link those files, you'll get an error stating intro_date() was redefined.

The definition of intro_date should exist in exactly one source file. Then the header would contain just the declaration.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

Typedef on struct declaration its "new name".

typedef struct matrix{
    int Val, Next;
} matrix;

Or specify on new instance creation explictly that it is struct:

struct matrix a[MAX][MAX];
1

Instead of

    void intro_date(int nr_elem, matrix a[MAX][MAX]){

use

    void intro_date(int nr_elem, struct matrix a[MAX][MAX]){
MarianD
  • 13,096
  • 12
  • 42
  • 54
0

Use Struct Keyword before that.

For C compilers, you have to user struct keyword whereas In C++, the struct keyword is optional. For Ease in C, you can typedef it.

typedef struct _matrix{
    int Val, Next;
}matrix;
Gurpreet Singh
  • 107
  • 1
  • 2