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

Discussion in 'C Programming' started by Louise Hoffman, Dec 14, 2008.

  1. 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
    }
     
    Louise Hoffman, Dec 14, 2008
    #1
    1. Advertising

  2. Louise Hoffman

    Zero Guest

    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
     
    Zero, Dec 14, 2008
    #2
    1. Advertising

  3. Louise Hoffman

    Zero Guest

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


    Zeh Mau
     
    Zero, Dec 14, 2008
    #3
  4. > ... and do not miss the include of the header file in the module...

    Thank you so much! Your the best! =)

    Hugs,
    Louise =)
     
    Louise Hoffman, Dec 14, 2008
    #4
  5. Louise Hoffman <> writes:

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

    --
    Ben.
     
    Ben Bacarisse, Dec 14, 2008
    #5
  6. > 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
     
    Louise Hoffman, Dec 15, 2008
    #6
  7. Louise Hoffman

    CBFalconer Guest

    Louise Hoffman wrote:
    >
    > 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Dec 16, 2008
    #7
  8. On 2008-12-16, Richard Heathfield <> wrote:
    >
    > It is a very bad idea to put definitions of any kind (other than
    > type definitions) into a header ...


    and macros.

    --
    Andrew Smallshaw
     
    Andrew Smallshaw, Dec 16, 2008
    #8
  9. Louise Hoffman

    CBFalconer Guest

    Richard Heathfield wrote:
    > CBFalconer said:
    >

    .... snip ...
    >
    >> Put extrn qualified definitions of the variables in the .h file.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    > 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'.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Dec 16, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    472
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    704
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    220
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    247
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    621
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page