Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Structure in a different File

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
whinson
Asked:
whinson
1 Solution
 
brunomsilvaCommented:
#include "fileb.c" ???
that seems a bit confusing, could you explain yourself a little better?
0
 
imladrisCommented:
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
 
Jaime OlivaresCommented:
save structure definition in an independent header file (.h)
include this header file in both file A and file B
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
whinsonAuthor Commented:
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
 
avizitCommented:
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
 
whinsonAuthor Commented:
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
 
avizitCommented:
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
 
whinsonAuthor Commented:
allright thank you
0
 
whinsonAuthor Commented:
Can i put multiple structures in the same header file and also can you typedef structures in the file
0
 
avizitCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now