theSongbird
theSongbird

Reputation: 241

error: unknown type name struct

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

Upvotes: 2

Views: 30213

Answers (4)

Gurpreet Singh
Gurpreet Singh

Reputation: 117

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;

Upvotes: 1

Armen Yeganyan
Armen Yeganyan

Reputation: 99

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

Upvotes: 2

dbush
dbush

Reputation: 223739

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.

Upvotes: 8

MarianD
MarianD

Reputation: 14131

Instead of

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

use

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

Upvotes: 1

Related Questions