How to import the structure defined in foo.c into bar.c.

Discussion in 'C Programming' started by hotadvice, Oct 26, 2005.

  1. hotadvice

    hotadvice Guest

    hello all

    Consider this:

    Suppose we have a an array of structure declared as following in
    file1.c

    structure foo{
    .......
    .......
    .......

    }FOO[50];

    ie the above defines a structure foo ( and an array of type foo ).


    Now in file2.c a function called FUNC needs to access this array.

    How do i do it.??

    What i tried (unsuccessfully though)---
    -- re-define the structure foo (exactly as in file1.c) in file2.c .I
    did this because i can not alter the file1.c and i do not have a header
    file.

    -- pass a pointer to array to FUNC
    The above did not work,the size of the structure changes(when i gdb
    through it)
    as the control transfers from file1.c to file2.c

    Though this worked:
    -- re-define the structure foo (in the same way) in file2.c
    -- passing the whole structure as an argument to FUNC.but this is
    inefficient i suppose.
    Got a better way??

    Any suggestions are appreciated.
    Thanx in advance.

    Aman
    hotadvice, Oct 26, 2005
    #1
    1. Advertising

  2. hotadvice

    Mike Wahler Guest

    "hotadvice" <> wrote in message
    news:...
    > hello all
    >
    > Consider this:
    >
    > Suppose we have a an array of structure declared as following in
    > file1.c
    >
    > structure foo{
    > ......
    > ......
    > ......
    >
    > }FOO[50];
    >
    > ie the above defines a structure foo ( and an array of type foo ).
    >
    >
    > Now in file2.c a function called FUNC needs to access this array.
    >
    > How do i do it.??
    >
    > What i tried (unsuccessfully though)---
    > -- re-define the structure foo (exactly as in file1.c) in file2.c .I
    > did this because i can not alter the file1.c and i do not have a header
    > file.
    >
    > -- pass a pointer to array to FUNC
    > The above did not work,the size of the structure changes(when i gdb
    > through it)
    > as the control transfers from file1.c to file2.c
    >
    > Though this worked:
    > -- re-define the structure foo (in the same way) in file2.c
    > -- passing the whole structure as an argument to FUNC.but this is
    > inefficient i suppose.
    > Got a better way??
    >
    > Any suggestions are appreciated.
    > Thanx in advance.
    >
    > Aman


    /* header.h */
    struct foo
    {
    int member;
    };

    void f2(struct foo *);


    /* file1.c */
    #include "header.h"

    void f1()
    {
    struct foo FOO[50] = {0};
    FOO[5].member = 42;
    f2(FOO, sizeof FOO / sizeof *FOO);
    }


    /* file2.c */
    #include <stdio.h>
    #include "header.h"

    void f2(struct foo *arg, size_t elems)
    {
    size_t i = 0;
    for(i = 0; i < elems; ++i)
    printf("%d\n", arg.member;
    }


    -Mike
    Mike Wahler, Oct 26, 2005
    #2
    1. Advertising

  3. hotadvice

    hotadvice Guest

    Re: How to import the structure defined in foo.c into bar.c.

    Mike Wahler wrote:
    > "hotadvice" <> wrote in message
    > news:...
    > > hello all
    > >
    > > Consider this:
    > >
    > > Suppose we have a an array of structure declared as following in
    > > file1.c
    > >
    > > structure foo{
    > > ......
    > > ......
    > > ......
    > >
    > > }FOO[50];
    > >
    > > ie the above defines a structure foo ( and an array of type foo ).
    > >
    > >
    > > Now in file2.c a function called FUNC needs to access this array.
    > >
    > > How do i do it.??
    > >
    > > What i tried (unsuccessfully though)---
    > > -- re-define the structure foo (exactly as in file1.c) in file2.c .I
    > > did this because i can not alter the file1.c and i do not have a header
    > > file.
    > >
    > > -- pass a pointer to array to FUNC
    > > The above did not work,the size of the structure changes(when i gdb
    > > through it)
    > > as the control transfers from file1.c to file2.c
    > >
    > > Though this worked:
    > > -- re-define the structure foo (in the same way) in file2.c
    > > -- passing the whole structure as an argument to FUNC.but this is
    > > inefficient i suppose.
    > > Got a better way??
    > >
    > > Any suggestions are appreciated.
    > > Thanx in advance.
    > >
    > > Aman

    >
    > /* header.h */
    > struct foo
    > {
    > int member;
    > };
    >
    > void f2(struct foo *);
    >
    >
    > /* file1.c */
    > #include "header.h"
    >
    > void f1()
    > {
    > struct foo FOO[50] = {0};
    > FOO[5].member = 42;
    > f2(FOO, sizeof FOO / sizeof *FOO);
    > }
    >
    >
    > /* file2.c */
    > #include <stdio.h>
    > #include "header.h"
    >
    > void f2(struct foo *arg, size_t elems)
    > {
    > size_t i = 0;
    > for(i = 0; i < elems; ++i)
    > printf("%d\n", arg.member;
    > }
    >
    >
    > -Mike


    This is ok
    But how one does it if the situation is like this
    and one can not modify the file1.c or create and
    add a new header file. Suppose one is dealing with old code
    or say testing some code.


    /* file1.c */
    struct foo
    {
    int member;
    }FOO[50];

    void f2(struct foo *);



    #include "header.h"

    void f1()
    {
    /* do something with FOO */


    f2(FOO, sizeof FOO / sizeof *FOO);
    }


    /* file2.c */
    #include <stdio.h>
    #include "header.h"

    void f2(struct foo *arg, size_t elems)
    {
    size_t i = 0;
    for(i = 0; i < elems; ++i)
    printf("%d\n", arg.member;
    }


    Now one would have to define the structure FOO in file2.c too.

    And if one does that then the problem is one gets different sizes for
    the same structure in file1.c and file2.c (checked during gdb through
    the code).
    and one is not able to access the structure elements.
    hotadvice, Oct 26, 2005
    #3
    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:
    3
    Views:
    374
    John Roth
    Jul 29, 2005
  2. Jonathan Bartlett
    Replies:
    7
    Views:
    442
    Peter Nilsson
    Jul 8, 2005
  3. Wejn
    Replies:
    2
    Views:
    109
    Michal
    Nov 29, 2003
  4. Max Williams
    Replies:
    10
    Views:
    193
    Max Williams
    Dec 15, 2007
  5. Gunnar Hjalmarsson
    Replies:
    12
    Views:
    293
    Darren Dunham
    Feb 24, 2005
Loading...

Share This Page