Sergio
Sergio

Reputation: 354

How to properly use 2 structs definition in one single C source file

I'd need some help with structs in C.

Basically I'm trying to create a Student and a Group struct definition. Keep in mind that Group struct will contain Student structs previously implemented.

Below my structs definition:

Student Struct: student.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_MATRICOLA 815010

typedef struct Student{
    int matricola;
    int voto_archi;
    char *turno;
}Student;

int generateVoto(){
    return (rand() % (30 - 18)) + 18;
}

char* generateTurno(int matricola){
    char *res = malloc(2*sizeof(char) + 1);
    if(matricola % 2 == 0)
        res = "T2";
    else    
        res = "T1";
    return res;
}

void initializeStudent(Student s, int matricola){
    s.matricola = matricola;
    s.voto_archi = generateVoto();
    if(s.matricola % 2 == 0){
        strcpy(s.turno, "T2");
    }
    else{
        strcpy(s.turno, "T1");
    }
}

void showStudent(Student s){
    printf("Matricola: %d Voto: %d Turno: %s\n", s.matricola, s.voto_archi, s.turno);
}

Student createStudent(int matricola){
    int voto = generateVoto();
    char *turno = generateTurno(matricola);
    Student s = {matricola, voto, turno};
    return s;
}

Group Struct: group.h

#include "headers.h"
#include "student.h"
#define MAX_MEMBERS 4

typedef struct Group{
    int groupId;
    Student *members;
    int numMembers;
    boolean closed;
}Group;

Group createGroup(int groupId){
    Group g;
    g.groupId = groupId;
    g.members = malloc(MAX_MEMBERS * sizeof(Student) + 1);
    g.numMembers = 0;
    g.closed = FALSE;
    return g;
}

void printGroup(Group g){
    int index = g.numMembers;
    if(index == 0)
        printf(RED "Group %d is EMPTY\n" RESET, g.groupId);
    else{
        for(int i = 0; i < MAX_MEMBERS; i++)
                showStudent(g.members[i]);
        printf("\n");
    }
}

Now even an empty main.c class containing only #include "student.h and #include "group.h would fail compiling but if we only add one of these two it works good.

Here's compiler's output:

Compiler's output if we try to compile C source file with those 2 files included

Now, at last, my question:

How to create a main.c class using both student.h and group.h files? What am I doing wrong?

Upvotes: 1

Views: 129

Answers (2)

tdao
tdao

Reputation: 17668

Now even an empty main.c class containing only #include "student.h and #include "group.h would fail compiling but if we only add one of these two it works good.

Apparently you lack the guard to protect your header files, which look something like this (for each .h file):

#ifndef STUDENT_H
#define STUDENT_H

// your header file goes here

#endif STUDENT_H

Alternately, you can use #pragma once at the beginning of each header file (which is supposedly a better and shorter way).

Upvotes: 2

Clifford
Clifford

Reputation: 93476

You need to wrap all your header files in "include guards" so that if the header content has already been included, in any subsequent inclusion the content is skipped to prevent redefinitions:

For example for group.h you might have:

#if !defined GROUP_H
#define GROUP_H

// all Header file content here...

#endif // GROUP_H

where the macro (GROUP_H) in this case must be unique throughout teh project - it is conventional to use a name based on the file name.

An alternative supported by many toolchaisn is to use the #pragma once directive:

#pragma once

// all Header file content here...

This is less portable, but more fool-proof that a traditional include guard.

Upvotes: 3

Related Questions