Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to import the structure defined in foo.c into bar.c.

Reply
Thread Tools

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

 
 
hotadvice
Guest
Posts: n/a
 
      10-26-2005
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

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-26-2005

"hotadvice" <> wrote in message
news: oups.com...
> 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[i].member;
}


-Mike


 
Reply With Quote
 
 
 
 
hotadvice
Guest
Posts: n/a
 
      10-26-2005

Mike Wahler wrote:
> "hotadvice" <> wrote in message
> news: oups.com...
> > 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[i].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[i].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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: __STDC_IEC_559__ (defined or !defined ?) Keith Thompson C Programming 0 08-17-2010 04:36 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57