Solved

Structure in a different File

Posted on 2004-10-20
10
264 Views
Last Modified: 2010-04-15
Hi,
    Say i have a structure defined in File A ex. structure struct();
    File B contains functions that File A uses.
    The problem the functions in File B have parameters that are structures in File A.
    How do i tell File B about the structures in File A.
Thanks
   
0
Comment
Question by:whinson
10 Comments
 
LVL 4

Expert Comment

by:brunomsilva
ID: 12363879
#include "fileb.c" ???
that seems a bit confusing, could you explain yourself a little better?
0
 
LVL 16

Expert Comment

by:imladris
ID: 12363886
The standard structure for C programs is to have such things in separate header files. That is, there would be a header file "s.h" which contains the definition for structure struct();

Then both A and B include file "s.h" so they both have access to the definition for structure struct.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12364009
save structure definition in an independent header file (.h)
include this header file in both file A and file B
0
 

Author Comment

by:whinson
ID: 12364665
Ok so i should remove the code for the structure from File A copy it into a just containing the code for a structure.  Create .h file with the prototype for the structure?  I know a prototype for a function is
ex. void function(int x);

Can you give me a an exmaple of a prototype for a structure?

Thanks
0
 
LVL 11

Expert Comment

by:avizit
ID: 12365870
basically you can have a header file say abcd.h

with the structure declaration

/***********abcd.h********/
struct abcd{
      char c;
      int a;
};


and then you can include this header wherever you need to define that structure or use it

#include "abcd.h"

also you would like to guard your header file from multiple inclusion , so you should prolly have your header file look like this

#ifndef ABCD_H
#define ABCD_H
struct abcd{
      char c;
      int a;
};

#endif  

check http://c2.com/cgi/wiki?IncludeGuard
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:whinson
ID: 12365931
Ok do i also need a .c file that looks like this

//c.file
struct abcd{
   char c;
   int a;
}

or is the headerall i need?
0
 
LVL 11

Expert Comment

by:avizit
ID: 12365989
no you don't need a c file with the content you mention ,

youjustneed to have a header similar to to what I have given and
say in your main.c you need to declare a structure of the type abcd  then you just include the header ffile and there you are
for example you can have a

xyz.c file


#include "abcd.h"

int main()
{
    struct abcd test;
    struct abcd *ptest;
     test.c = "a";
 etc etc etc etc .

}
0
 

Author Comment

by:whinson
ID: 12366001
allright thank you
0
 

Author Comment

by:whinson
ID: 12366080
Can i put multiple structures in the same header file and also can you typedef structures in the file
0
 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
ID: 12366123
yes you can

the following program will ilustrate it

---------file abcd.h----------------
#ifndef ABCD_H
#define ABCD_H

struct a1{
  int a;
  char b;
};

typedef struct a2{      
  int a;
  int b;
}A2;               /**********this is a typedef ***********/

----------------------------------------

---------file abcd.c-------------
#include <stdio.h>
#include "abcd.h"     /*********you include the file whereever you need to use the structures ********/

int main(){

  struct a1 aone;
  A2 atwo;        /************you can use the typedefs ****************/
  aone.a = 5;
  aone.b = 'c';

  atwo.a = 1;
  atwo.b = 2;

  printf("aone: a: %d\t b: %c\n",aone.a, aone.b);
  printf("atwo: a: %d\t b: %d\n",atwo.a, atwo.b);
  return 0;
}

-------------------------------

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
posix semaphore deadlock 13 114
Which checksum is this? 7 139
C hashtable library 3 81
xamarin c# deserialize Json containing nested object 2 80
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now