Solved

Structure in a different File

Posted on 2004-10-20
10
265 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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

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

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…
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 how to create, access, and change arrays in the C programming language.

832 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