Solved

Structure in a different File

Posted on 2004-10-20
10
266 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 

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
 

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

830 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