How to split this c file up in .c and .h ?

L

Louise Hoffman

Dear readers,

I can't figure out how to split this file up into a .c and a .h file.
Can someone explain to me, what should go where?

Lots of love,
Louise =)

#ifndef FILE_NAME
#define FILE_NAME "ovn.sim"
#endif

struct sized_array_struct {
int n;
double *x;
double *y;
};

typedef struct sized_array_struct raw_t;

raw_t read_file() {

// lots of code here
}
 
Z

Zero

Header file (declarations, typedefs)...

#ifndef FILE_NAME
#define FILE_NAME "ovn.sim"
#endif

struct sized_array_struct {
int n;
double *x;
double *y;

};

typedef struct sized_array_struct raw_t;

raw_t read_file(void);

Module:

Implementation of functions:
raw_t read_file(void)
{
// lots of code
}

Zeh Mau
 
L

Louise Hoffman

... and do not miss the include of the header file in the module...

Thank you so much! Your the best! =)

Hugs,
Louise =)
 
B

Ben Bacarisse

Louise Hoffman said:
I can't figure out how to split this file up into a .c and a .h file.
Can someone explain to me, what should go where?

That is not easy to answer since it depends in part on what you doing
(in particular what port of the code need to know what things). For
example, I can't tell if the define of FILE_NAME is needed by the code
that uses read_file, or by the code that implements read_file or by
both.

Here is one possible way:

----- in file sized_array.h -----
#ifndef H_SIZED_ARRAY
#define H_SIZED_ARRAY

struct sized_array_struct {
int n;
double *x;
double *y;
};

typedef struct sized_array_struct raw_t;

raw_t read_file(void);
#endif
---------------------------------

The last-but-one line is a declaration (and a prototype) for the function
provided by the sized_array code. When a function takes no arguments,
it is better to state that explicitly with as I have done here (and below).

The lines at the top and the matching endif are an include guard -- an
optional extra that permits on to be more carefree about how this file
in included into others since its content can never be included twice.

----- in file sized_array.c -----
#include "sized_array.h"

#ifndef FILE_NAME
#define FILE_NAME "ovn.sim"
#endif

raw_t read_file(void) {

// lots of code here
}
 
L

Louise Hoffman

The last-but-one line is a declaration (and a prototype) for the function
provided by the sized_array code.  When a function takes no arguments,
it is better to state that explicitly with as I have done here (and below).
Noted.

The lines at the top and the matching endif are an include guard -- an
optional extra that permits on to be more carefree about how this file
in included into others since its content can never be included twice.

I like this include guard. Thanks =)

Hugs,
Louise
 
C

CBFalconer

Louise said:
I can't figure out how to split this file up into a .c and a .h
file. Can someone explain to me, what should go where?

Wrong question. You are writing a C file. You want other source
files to be able to access functions and variables (and possibly
more) from that C file. Put the function prototypes in the .h
file. Put extrn qualified definitions of the variables in the .h
file.
 
C

CBFalconer

Richard said:
CBFalconer said:
.... snip ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^
It is a very bad idea to put definitions of any kind (other than
type definitions) into a header (except in cases where the header
is included only by exactly one source file; in such cases, whether
the existence of the header itself is wise is a matter of opinion).

Unless you want to replace 'definition' with 'declaration'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top