How to integrate two c files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • neha_chhatre@yahoo.co.in

    How to integrate two c files

    hello
    i have a problem... have written two separate c files named read.c
    and twinkle.c

    the file read.c, after running stores some values in an array name
    average[] and it has a count variable k......

    now i need to read both average[] and the variable k from the
    twinkle.c file

    How do i integrate the two files r in short..how do i read the values
    of average[] and count variable k from twinkle.c

    Please help me urgently....... .......
  • Michael Mair

    #2
    Re: How to integrate two c files

    neha_chhatre@ya hoo.co.in wrote:
    hello
    i have a problem... have written two separate c files named read.c
    and twinkle.c
    >
    the file read.c, after running stores some values in an array name
    average[] and it has a count variable k......
    >
    now i need to read both average[] and the variable k from the
    twinkle.c file
    >
    How do i integrate the two files r in short..how do i read the values
    of average[] and count variable k from twinkle.c
    >
    Please help me urgently....... .......

    -- program.c --
    #include "read.h"
    #include "twinkle.h"

    int main(void)
    {
    int k = 0;
    int* array = 0;
    MyRead(&array, &k);
    if (NULL == array)
    {
    MyTwinkle(array , k);
    }
    MyReadCleanup(& array, k);
    return 0;
    }
    ----
    -- read.h --
    void MyRead (int **pArray, int *pK);
    void MyReadCleanup (int **pArray, int k);
    ----
    -- twinkle.h --
    void MyTwinkle (int *array, int k);
    ----

    Fill in twinkle.c, read.c as necessary.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • Mark Bluemel

      #3
      Re: How to integrate two c files

      neha_chhatre@ya hoo.co.in wrote:
      hello
      i have a problem... have written two separate c files named read.c
      and twinkle.c
      >
      the file read.c, after running stores some values in an array name
      average[] and it has a count variable k......
      >
      now i need to read both average[] and the variable k from the
      twinkle.c file
      >
      How do i integrate the two files r in short..how do i read the values
      of average[] and count variable k from twinkle.c
      >
      Please help me urgently....... .......
      Are you (yet) another of those people trying to teach themselves C
      without reading a decent book or tutorial?

      Comment

      • MisterE

        #4
        Re: How to integrate two c files


        <neha_chhatre@y ahoo.co.inwrote in message
        news:5d674a5b-4b8e-4939-8135-9d116bf44cab@f4 7g2000hsd.googl egroups.com...
        hello
        i have a problem... have written two separate c files named read.c
        and twinkle.c
        >
        the file read.c, after running stores some values in an array name
        average[] and it has a count variable k......
        >
        now i need to read both average[] and the variable k from the
        twinkle.c file
        >
        How do i integrate the two files r in short..how do i read the values
        of average[] and count variable k from twinkle.c
        >
        Please help me urgently....... ......
        The easiest and most nasty way is to declare the variables as global
        variables. So you would have somesomething like:
        char avarage[100];
        int index=0;
        in read.c,
        then in twinkle.c you can use them by defining:
        extern char average[100];
        extern int index;

        the extern means they are externally declared in another file.


        Comment

        • santosh

          #5
          Re: How to integrate two c files

          Mark Bluemel wrote:
          neha_chhatre@ya hoo.co.in wrote:
          >hello
          >i have a problem... have written two separate c files named read.c
          >and twinkle.c
          >>
          >the file read.c, after running stores some values in an array name
          >average[] and it has a count variable k......
          >>
          >now i need to read both average[] and the variable k from the
          >twinkle.c file
          >>
          >How do i integrate the two files r in short..how do i read the values
          >of average[] and count variable k from twinkle.c
          >>
          >Please help me urgently....... .......
          >
          Are you (yet) another of those people trying to teach themselves C
          without reading a decent book or tutorial?
          To be fair K&R2, Harbison & Steele, K.N. King's C Programming: A Modern
          Approach are typically not stocked in bookshops here. And if they are,
          they are usually far beyond the budget of most people. Their best
          choice is to shop for a used version on Amazon and similar sites. Local
          authors are cheap enough, but unfortunately, both in cost and quality.

          Comment

          • David T. Ashley

            #6
            Re: How to integrate two c files

            <neha_chhatre@y ahoo.co.inwrote in message
            news:5d674a5b-4b8e-4939-8135-9d116bf44cab@f4 7g2000hsd.googl egroups.com...
            i have a problem... have written two separate c files named read.c
            and twinkle.c
            >
            the file read.c, after running stores some values in an array name
            average[] and it has a count variable k......
            >
            now i need to read both average[] and the variable k from the
            twinkle.c file
            >
            How do i integrate the two files r in short..how do i read the values
            of average[] and count variable k from twinkle.c
            Generally speaking, .H files allow you to declare an interface to the
            corresponding .C file, which may include global variables.

            When you start using .H files, there are a few things you want to provide
            for:

            a)A given include file should "stand alone", i.e. not require the inclusion
            of any other files. (The generally requires that it itself include files,
            and requires some mechanism that deals with multiple inclusion).

            b)You want to avoid identifying the characteristics of anything in more than
            one place (change and consistency risk).

            c)You want full prototype linkage--you want the definition of a function
            checked against the prototype, and the prototype checked against the
            invocations.

            Here are some sample files that may be a suitable example:

            Download ESRG Tool Set for free. A Windows and Unix tool set for embedded system development, an accompanying book, and all source code.


            Download ESRG Tool Set for free. A Windows and Unix tool set for embedded system development, an accompanying book, and all source code.


            Unfortunately the examples above don't have any global data, but it can be
            placed in the .H file using the DECMOD_ ... construct.

            For example:

            DECMOD_MYMODULE int bart_simpsons_i q_readings[5]
            #ifdef MODULE_MYMODULE
            = {32, 49, 57, 23, 33}
            #endif
            ;

            Note that this expands a bit differently when compiling the owning module
            rather than others.

            Dave.

            Comment

            • Flash Gordon

              #7
              Re: How to integrate two c files

              David T. Ashley wrote, On 21/02/08 17:45:
              <neha_chhatre@y ahoo.co.inwrote in message
              news:5d674a5b-4b8e-4939-8135-9d116bf44cab@f4 7g2000hsd.googl egroups.com...
              >i have a problem... have written two separate c files named read.c
              >and twinkle.c
              >>
              >the file read.c, after running stores some values in an array name
              >average[] and it has a count variable k......
              >>
              >now i need to read both average[] and the variable k from the
              >twinkle.c file
              >>
              >How do i integrate the two files r in short..how do i read the values
              >of average[] and count variable k from twinkle.c
              >
              Generally speaking, .H files allow you to declare an interface to the
              corresponding .C file, which may include global variables.
              On some implementations the case of the extension affects how the
              compiler treats it so, traditionally, that should be .h and .c if you
              are programming in C. Note that on some of these implementations .C will
              cause the code to be compiled as C++.
              When you start using .H files, there are a few things you want to
              provide for:
              >
              a)A given include file should "stand alone", i.e. not require the
              inclusion of any other files. (The generally requires that it itself
              include files, and requires some mechanism that deals with multiple
              inclusion).
              >
              b)You want to avoid identifying the characteristics of anything in more
              than one place (change and consistency risk).
              >
              c)You want full prototype linkage--you want the definition of a function
              checked against the prototype, and the prototype checked against the
              invocations.
              To achieve all of c you need to include the header file in the source
              file that provides the definitions. I.e. a.c should #include a.h
              Here are some sample files that may be a suitable example:
              >
              Download ESRG Tool Set for free. A Windows and Unix tool set for embedded system development, an accompanying book, and all source code.

              >
              >
              Download ESRG Tool Set for free. A Windows and Unix tool set for embedded system development, an accompanying book, and all source code.

              >
              >
              Unfortunately the examples above don't have any global data, but it can
              be placed in the .H file using the DECMOD_ ... construct.
              >
              For example:
              >
              DECMOD_MYMODULE int bart_simpsons_i q_readings[5]
              #ifdef MODULE_MYMODULE
              = {32, 49, 57, 23, 33}
              #endif
              ;
              >
              Note that this expands a bit differently when compiling the owning
              module rather than others.
              Many people don't like that style and would prefer a simple declaration
              in the header and definition in the .c, i.e.

              a.h
              extern int bart_simsons_iq _readings[5];

              a.c
              #include <a.h>
              int bart_simsons_iq _readings[5] = {32,49,23,33};

              The compiler is guaranteed to report any mismatch because of the
              inclusion of a.h so there is no risk of a type mismatch being missed.
              --
              Flash Gordon

              Comment

              Working...