Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: unknown type name struct

Tags:

c

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;
    }
}
like image 621
theSongbird Avatar asked Jan 18 '26 15:01

theSongbird


2 Answers

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.

like image 175
dbush Avatar answered Jan 20 '26 06:01

dbush


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];
like image 28
Armen Yeganyan Avatar answered Jan 20 '26 06:01

Armen Yeganyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!