Thread: typedef global

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    typedef global

    hi,

    i am just trying to find out more about using one declared struct type across multiple files,
    basically if i had

    Code:
    typedef struct AllPoints{
         float x,y,z;
    }Allpoints;
    and now i declare

    Code:
    Allpoints Globalpoints;
    how would i make this instance available across multiple .c files.
    Would it entail using extern .

    any advice appreciated

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You write
    Allpoints Globalpoints;
    in one .c file, and then you typically add the same to a header with the extern keyword:
    extern Allpoints Globalpoints;
    Include that header and now multiple .c files can use the variable.

    But I would urge you to avoid global variables if necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    thanks for clarifying,

    i have read elsewhere about trying to avoid global variables in c type programming,
    and yes generally i agree with you, as it can become problematic when programs become bigger and you accidently re-use names etc.
    it is something i have to deal with coding in php/javascript at times, i am still beginner at c though,

    the main reason i want to create some kind of global typedef is that i am interested in opengl and i am experimenting with a 3d environment, as well as objects that can be manipulated by other factors, which are spread among other files. For this reason i thought it might be convenient to store this data in a global variable, however ....as i say i am still experimenting, and as i build code i will probably realize better solutions along the way.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is acceptable. But I can suggest you keep all the global data tucked away in one or few structs so you don't clutter up too much.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    you very quick to reply, wow

    basically yes, my example above is actually just a simplified example, i will probably not have a global struct for vertices, but will try have a global struct for a object type which will encompass instances of properties that make up that object,

    so a global struct for object which contains perhaps other struct instances for vertices, faces etc. i learnt how to use linked lists about two weeks ago, and these will definitely come in handy.

    in php i would normally use a class object, and if i use c++ i could do that too, but at this point i first want to get a foundation in c programming then i will begin to learn more about c++.

    i started off my opengl programming in very inconvenient ways using single variables and arrays to learn things, as it was easier at the time, and i could learn how things work in opengl without knowing all about c program structure, however now i am trying to take what i have learned about opengl so far, and build it into a more acceptable and convenient program structure, so i spend my spare hours learning about c stuff.

    thanks again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, I don't know if a linked list is suitable for what you're trying to do. But then again, I don't know exactly what you need it for.
    I wouldn't say you need a C foundation to use C++ and I'm sure many would agree. Many C++ books teach some basic C concepts before moving on to the C++ part, but even so, knowing C is not a requirement for programming in C++.
    I would just say you should take the step directly into C++. It would save a lot of grief.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    this i think will be my last reply here, although i do enjoy the talk,
    i come from not such a big town in south africa, and while i work as a web developer to earn my rent, i actually dont know that many programmers, the people i work for are actually very business orientated and most of their programming revolves around visual foxpro stuff and then php , i have only briefly looked at foxpro,
    i am actually the only person in the office who has a decent understanding javascripting as i used my spare time to learn the DOM, but anyhow what i wanted to say was that i dont actually know anybody who programs in c, so all that i know so far i learn from the net, my local library is not equipped with decent books, and i do want to get a book on c programming but i would have to order it and well wont get a chance to peruse and check if it actually has decent content and not stuff i could just browse on the web,
    i will pretty soon look at more c++, but i initially started with c and i am just that type of person that likes to push it through, if i dont i will always be asking myself about it, i actually intend on learning about python as well, as i do have a very basic knowledge of that too,
    it is not so much about because c is lower level and all that, it just is i suppose, and even more strange, i actually didn't even study any computer courses, i studied fine art, but then after university i just became hooked to programming when i began to learn php - this was my introduction into this stuff, and i have totally enjoyed the challenges, and don't mind taking the time to build foundations in different aspects of programming, but thanks again, this is my story about c programming, later.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There's a recommended books thread if you need help finding a suitable book for learning.
    It's recommended, also, because books cover much more than tutorials and often explain things more in depth.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    guess i amback,

    yesterday i spent a few hours rewriting a small opengl program mainly to practice what we have been discussing.
    for the most part, if i think about a situation in the program, i can just pass pointers to structs in a function, something like...
    Code:
    typedef struct Screen{
           int high;
           int wide;
    }Screen
    
    void do_screen_coords(Qscreen *screen){
    
        #ifdef _WIN32
        SDL_SysWMinfo i;
        SDL_VERSION( &i.version );
        if (!SDL_GetWMInfo(&i))  {
                    fprintf(stderr, "Failed to get WM Info!\n");
                    exit(1);
           }
        HWND hwnd = i.window;
        screen->wide = (GetSystemMetrics( SM_CXSCREEN ))-5;
        screen->high = (GetSystemMetrics( SM_CYSCREEN ))-60;
        SetWindowPos(hwnd, HWND_TOP, 0, 0, screen->wide, screen->high, SWP_DRAWFRAME);
        #else
        screen->wide = 640;
        screen->high = 480;
        #endif
        //fprintf (stdout,"old(%i,%i)\n", screen->wide,screen->high);
    }
    
    
    
    //then later
    Qscreen initscreen;
        do_screen_coords(&initscreen);
    
     mywin = SDL_SetVideoMode(initscreen.wide, initscreen.high, 16, SDL_OPENGL |SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);
    i am sure there are better ways to do this, but for the most part i was able to use similar concepts to above to pass information around in the program,

    then i got to a part where i have a glRotate()&glTranslate(); that manipulates the matrix in one function in one file
    and an eventhandler to manipulate the rotate/translate variables in another file;

    these variables are stored as a struct instance
    Code:
    typedef struct WVector{
        float tx,ty,tz;
        float rx,ry;
    }WVector;
    
    extern WVector *G_move;
    but i began reading up a bit more because i am sure i can do this differently and better without using the extern, basically what i am going to try later today after work, is using malloc,

    but what i want to know is, if I allocate memory for a pointer to the WVector struct, would this memory portion be available across all files without using the extern declaration,up until i free it.

    if so that would be great, any advice?am i understanding this correctly?

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > would this memory portion be available across all files without using the extern declaration,up until i free it.
    Yes.

    In my little engine, I do the same. However I'm looking for a better idea, since it's not really thread-safe. Ie

    render.h
    Code:
    #ifndef INCLUDED_RENDER_H
    #define INCLUDED_RENDER_H
    
    extern struct render_s
    {
        int w, h;
        int fps;
    } render;
    
    #endif /* INCLUDED_RENDER_H */
    render.c
    Code:
    #include "render.h"
    
    struct render_s render;
    
    void render_init(void)
    {
        render.w = 800;
        render.h = 600;
        render.fps = 60;
    }
    Starting to look like home-brand C++ Another method would be to control access to the data, ie

    render.c
    Code:
    static struct render_s render;
    
    struct render_s * render_get_struct(void)
    {
        if(something == 10)
            return &render;
        return NULL;
    }
    Last edited by zacs7; 06-12-2008 at 07:14 AM.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    been looking about, and researching for alternatives for the last while,
    for the most part i am quite happy to use extern variable struct, but as elysian pointed out to me it is also a good idea to tuck things away,

    one thing i encountered today was the notion of forward declaration, which i think might help out, in this sense you could have various base
    structs that work with data in various ways and have them handy in whatever header files you needed them for, then in another header file you could declare a struct that nests together struct instances of whatever structs you need as globals, then you can declare this struct as an extern and have it handle your globals - you won't have to worry so much about thread safety that way,
    you don't have to worry about including the headers for the struct as you can forward declare them,
    at least this is what i am guessing at, i want to try illustrate here, and perhaps think this concept through myself, so here goes....

    Code:
    //for instance - you have a mesh handling mesh.c file and its 'direct header' mesh.h
    
    //in mesh.c you will be working with vertices,faces,etc
    //so a handy struct to include in mesh.h might be.....
    
    typedef struct VertexData{
              float vert[3];
              float norm[3];
    }VertexData;
    
    //you might also see a function like this in mesh.c
    void calc_norm(VertexData *vnorm);
    
    
    //then in a global_object.h file you could do the following
    
    struct VertexData;
    
    typedef struct my_GlobalObject{
    
                struct VertexData *globalvertex;
    
    }my_GlobalObject;
    
    extern my_GlobalObject glob; //glob can now handle whatever globals you need to.
    this is pretty lame example, but i am sure that the concept put into practice within a program with its relative tasks could potentially become a very ideal way to work with global manipulation. I am going to try this concept out tonight and see if it can work, as I have actually only thought about it.

    anyway feedback appreciated.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Forward declarations are essentially a way to tell the compiler a given data type exists, but not tell it the complete definition or declaration of the type.
    This makes it possible to use a type of X inside, say, a type of Y, without including the header for type X.
    However, you must remember that it only works with blueprints and in your real code where you create and access the type X, or the type Y where X is also included, you will need to include the header to have the complete definition or declaration of type X (and Y).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    thanks, did not realise that - would probably have found out the hard way, but essentially if i do include the said header for the forward declaration wherever needed in a .c file, would using the concept above (if i organize well) be a sufficent solution to handle a more complex global instance that gets manipulated across many files.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't necessarily need to do much more than just include the header for type X.
    However, if you do that and it still says X is undefined (assuming you have placed header guards everywhere), then you may need a forward declaration.

    But if the type never uses the type X in the first place (no code), then certainly, you can use forward declarations. You just need to include the header X in all the source files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM