2

i just started C and im not very familiar with how this works. I got a file stud.h:

struct stud{};
void inc_stud_pos(){};
int get_stud_pos(){};

and stud.c:

#include "stud"

#define STUDENT_SIZE 20

struct stud{
    char stud_id[MAX_STR_LEN];
    char stud_name[MAX_STR_LEN];
    struct grade Grade[MAX_STRUCT];
    struct income Income[MAX_STRUCT];
};

int stud_location = 0;

struct stud students[STUDENT_SIZE];

void inc_stud_pos(){
    stud_location++;
}

int get_stud_pos(){
    return stud_location;
}

And now i want from another file to access some of the functions inside stud.c. Lets say i want to use STUDENT_SIZE in another file whats the syntax? Do i need to use stud.STUDENT_SIZE? Or when i include "stud" i have access to STUDENT SIZE? And if i want to lets say make an array of students:

struct stud students[STUDENT_SIZE];

Can i access that normally like students[5].stud_id ?

Thanks!

Kan
  • 31
  • 7

2 Answers2

2

The header file (.h) is the programmer's interface to your module. Anything a user (programmer) of your module needs to know to use your module should be in there, but put the actual implementations in the associated .c file. Generally, you put the function prototypes (e.g. int f(void);) in the .h file and the definitions (the implementations, e.g. int f(void) { return 1; }) in the .c file. The linker matches the prototype to the definition because the function signatures match (the name, argument types, and return type). One way to think about it is the .h file as what you share with everyone, and the .c file as what you keep to yourself.

If you want a user of your module to have access to STUDENT_SIZE, then put

#define STUDENT_SIZE 20

in your .h file instead of your .c file. Then, any file with

#include "stud.h"

would have your macro for STUDENT_SIZE. (Notice the file name is "stud.h" not "stud".)

Lets say i want to use STUDENT_SIZE in another file whats the syntax? Do i need to use stud.STUDENT_SIZE?

No. You might be confusing this with namespaces, which C does not have in the same sense as other languages like C++, Java, etc. Just use STUDENT_SIZE. (This is why it is important to not use existing identifiers, and also why many people frown upon macros.)

... i want from another file to access some of the functions inside stud.c.

If you want another file to have access to some function in stud.c, then put that function's prototype in stud.h. This is precisely what the header file is for.

... if i want to lets say make an array of students:

struct stud students[STUDENT_SIZE];

Can i access that normally like students[5].stud_id ?

As you have written your header file, no. You might know some struct stud exists, but you don't know what the members are called (i.e. you wouldn't know stud_id exists). The reason is that the user of your module sees only what is in the header file, and you defined no members of the struct in your header file. (I'm surprised what you have written even compiles, because you define struct stud twice, once empty {}, and once with members.) Again, if you want the programmer user to have access to the members of struct stud, its definition

struct stud {
    char stud_id[MAX_STR_LEN];
    char stud_name[MAX_STR_LEN];
    struct grade Grade[MAX_STRUCT];
    struct income Income[MAX_STRUCT];
};

belongs in your header file (along with whatever those other macros are). Without knowing this, the programmer (and the compiler) has no idea what the members of struct stud are.

(You didn't ask about this, but if you want to share access to a global variable in your .c file, then declare it with the keyword extern in your .h file, and without extern in your .c file. See How to correctly use the extern keyword in C for more.)

Community
  • 1
  • 1
e0k
  • 6,961
  • 2
  • 23
  • 30
  • Thank you very much, this is exactly what i needed to know. Im used to Java and C feels like im carving in stone and i pretty much try to do this blind. But now everything is clear, so thanks a lot. – Kan Dec 04 '16 at 22:32
1

You cannot access STUDENT_SIZE with how the header file is written. The header file bridges and extends the scope of the aforementioned items:

struct stud{}; //this should be fixed btw
void inc_stud_pos();
int get_stud_pos();

If you want to extend the scope of STUDENT_SIZE then instead of declaring it in the .c file put it in the .h file. If you do this however you will have to remove the line of code where you declare STUDENT_SIZE in the .c file.

dovedevic
  • 673
  • 2
  • 14
  • 33