473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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

Nov 15 '05 #1
2 3209

"hotadvice" <am******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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
Nov 15 '05 #2

Mike Wahler wrote:
"hotadvice" <am******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MackS | last post by:
Hi I'm new to Python, I've read the FAQ but still can't get the following simple example working: # file main_mod.py: global_string = 'abc' def main():
0
by: Doug R | last post by:
Hello, I have a system that I am writing to automaticly import Credit Transaction data into a SQL Server 2000 Database. I am using a VB.Net application to detect when the file arives and prep...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
2
by: john | last post by:
I have 400 different Excel-spreadsheetfiles, same structure, all with only one record in it, and all residing in the same folder. Every now and then new Excel files are being added. In my Access...
12
by: Alan Isaac | last post by:
Are relative imports broken in 2.5? Directory ``temp`` contains:: __init__.py test1.py test2.py File contents: __init__.py and test2.py are empty test1.py contains a single line::
2
by: rfdes | last post by:
Hi- I could use some help resolving a problem. This explanation will be lengthy as I am trying to describe my problem without posting real code. I currently have a Win32 console application & a...
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.