modular c programming

This is a discussion on modular c programming within the C Programming forums, part of the General Programming Boards category; hi; i have a project with many modules. my questions are : -if functions of module_superior call functions of module_inferior ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    modular c programming

    hi;
    i have a project with many modules. my questions are :
    -if functions of module_superior call functions of module_inferior can i access global variable of module_superior in module_inferior(the superior module in an inferior module) by decalaring them as extern?
    -if functions of module_superior call functions of module_inferior can i define a global variable in module_inferior then modify it in module_superior then read it in module_inferior?
    which solution is the best?
    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The _BEST_ approach is to avoid global variables as much as possible.

    Pass structures between different modules (as pointer to the struct, generally).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    thx for reply but i haven't other choice i must use global variables.so which solution seems to work better?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is a very rare case. There is no standard for where you should place your global variables. I would probably put them in the "superior" module and have extern declarations in a header file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    thanks.here's an example:
    Code:
    //module sup
    #include "module_inf.h"
    int var;
    //modify  var
    void modif_var(void){
    var=0;
    //call of a function of module1 which reads var and do other instructions
    module1_func();
    }
    ///////////////////////////
    //module inf
    extern int var;
    void module1_func(void){
    read(var);//read var
    }
    sould this work?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes. I would, however recommend the use of an include file to declare the extern for var.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modular Programming
    By St0rM-MaN in forum C Programming
    Replies: 7
    Last Post: 05-10-2007, 03:56 AM
  2. Modular
    By loopshot in forum Game Programming
    Replies: 7
    Last Post: 01-20-2006, 07:24 PM
  3. Modular math
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-01-2003, 09:35 AM
  4. Modular Division problem
    By Malek in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2003, 07:08 PM
  5. Completely modular motherboards?
    By SMurf in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-23-2003, 12:56 PM

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