Solved

Structure in a different File

Posted on 2004-10-20
10
263 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

708 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

17 Experts available now in Live!

Get 1:1 Help Now