External structure?

This is a discussion on External structure? within the C Programming forums, part of the General Programming Boards category; how can i use a structure in multiple source files? this isnt covered any where...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    External structure?

    how can i use a structure in multiple source files? this isnt covered any where

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: External structure?

    Originally posted by mart_man00
    how can i use a structure in multiple source files? this isnt covered any where
    Are you sure it's not covered?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    what do you mean "isnt covered anywhere"?

    anyway, define the struct in a .h file and #include it wherever nessecary
    hello, internet!

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: External structure?

    Originally posted by mart_man00
    how can i use a structure in multiple source files? this isnt covered any where
    I'll give you a quick example. Hopefully this will help you a little bit. Let's say you have 3 files, main.c , extern.c and vector.h lets have a look.

    main.c
    Code:
    #define MAIN_C
    #include "vector.h"
    
    int main( void )
    {
      /* Set the values */
      myVector.x = 0.0f;
      myVector.y = 50.0f;
      myVector.z = 0.0f;
    
      /* Display values */
      OutputVector( &myVector );
      
      /* Change our vector externally */
      FillVector( );
      
      /* Display values */
      OutputVector( &myVector );
    
      return 0;
    }
    vector.h
    Code:
    #ifndef VECTOR_H
    #define VECTOR_H
    
    /* Simple 3D Vector structure */
    struct Vector3D
    {
      float x;
      float y;
      float z;
    };
    
    /* If we are in the main.c file, we don't use extern .... */
    #ifdef MAIN_C
    #define GLOBAL 
    
    /* If we are in some other file, we want to use extern */
    #else
    #define GLOBAL extern
    #endif
    
    /* Here is our external structure */
    GLOBAL struct Vector3D myVector;
    
    /* Prototype the functions */
    void FillVector( void );
    void OutputVector( struct Vector3D *pV );
    
    #endif /* VECTOR_H */
    extern.c
    Code:
    #include "vector.h"
    #include <stdio.h>
    
    /* Fill in external vector with some values */
    void FillVector( void )
    {
      myVector.x = 123.0f;
      myVector.y = 444.0f;
      myVector.z = 12.0f;
    }
    
    /* Output a given vector */
    void OutputVector( struct Vector3D *pV )
    {
      printf( "( %f, %f, %f ) \n", pV->x, pV->y, pV->z );
    }
    I tryed to comment this code as much as possible. Basically you make a definition for your main.c so that it won't be declared external. However, when you include vector.h other times you don't have MAIN_C defined so it WILL use the extern keyword. Just write back if you have any questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM
  5. Ask about these "unresolved external symbol" error
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21