Thread: Global Structure

  1. #1
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30

    Global Structure

    I am desperately stuck please help.....

    I am trying to use a structure which can be accessed and changed by my main c file as well as some header files which I am creating to go with it.

    I have tried a few methods, but have been unsuccessful.

    My last atempt took the following form:


    The main file included two header files, one header file which held the structure definition and declared the array of structures, the other held a function which added data elements to the structure. The main file also needs to be able to add data and read data to and from the structure.

    For example:

    main.c
    ------------------

    Code:
    #include <header1.h>
    #include <header2.h>
    
    int main(void){
          
    MyFunction();
    
    }

    header1.h
    -------------------

    Code:
    struct Data{
    int data1;
    char data2[20];
    };
    
    struct Data structArray[20];

    header2.h
    ------------------

    Code:
    #include <header1.h>
    
    void MyFunction();
    
    void MyFunction(){
    
    structArray[0].data1 = 2;
    structArray[0].data2[0] = 's';
    }


    Any help is much appreciated, thanks in advance

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    get rid of the chevrons in your own header files. They should also be in the same directory as your main file.

    #include <stdio.h> // adds a library to program or header
    #include "myheader.h" // adds a self made header to your program
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    Ok, I have done that.... that didn't appear to have any effect. I think my compliler must have ignored that I had written in those chevrons because it was finding the structure definition from the header file without any problem.

    The error that I am being given when I try to compile is:

    redefinition of 'struct mystructure'

    For reference I am using Dev C++ 4.9.9.1

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Don't declare global variables or functions in header files.

    Header files should ONLY contain definitions of data types,
    function prototypes and C preprocessor commands.

  5. #5
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    ok... so what is the best way to have a structure that I can access in my main program and have functions in header files that use the structure??

    I have been experimenting with different methods, but not yet found one that works.

    One method I was trying is to have the structure declared in the main *. file. But I couldn't figure out how to define the pointer in the function prototype?? Everything I was trying failed. I figured that the header file must need access to the structure definition to know what kind of data and data sizes it is pointing to. This is what led me to define the structure in a header file which both could access.

    Many thanks for the help so far

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void MyFunction();
    Put this in header1.h

    Then rename header2.h to be header1.cpp

    Then remove #include <header2.h>
    from main.cpp

    Then add header1.cpp to the list of source files in the project.

    There's a multi-file project FAQ you know...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    While you're at it, rename all of your ".cpp" files to ".c" and start compiling as C instead of C++.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Heh - so it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    Thanks so much... that worked a treat.

    I shall be sure to read the FAQ.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Structure question.
    By tdep in forum C Programming
    Replies: 6
    Last Post: 07-04-2007, 05:59 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Global structure def's
    By earth_angel in forum C Programming
    Replies: 3
    Last Post: 06-14-2005, 08:56 AM
  4. Problems with Global Structure
    By bijju_savan in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 03:37 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM