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

H

hotadvice

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
 
M

Mike Wahler

hotadvice said:
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
 
H

hotadvice

Mike said:
hotadvice said:
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.
 

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,021
Latest member
AkilahJaim

Latest Threads

Top