Thread: Structure variable : how to set as generic ?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    11

    Structure variable : how to set as generic ?

    Hi All,

    The below is the snippet of code which i am working on .
    i am wriiting a switch case to set each of the variables in structure .

    Note : REG_ADDR is address of some say (0x1000) .

    i will get channel as input ,based on that i am setting the registers as shown in "Compute" function.

    Code:
    /* Some header definitions */ 
    ---------------------------------
    struct REG_TYPE_tag
    {
        int    COUNT;                     
    }REG_TYPE_T;
    
    
    typedef struct TOTAL_REGS_tag
    { 
        REG_TYPE_T         SM0;
        REG_TYPE_T         SM1;
     }TOTAL_REGS_T;
    
    #define REG_ADDR (*( volatile TOTAL_REGS_T* )(0x1000 ))
    ----------------------------------------------------------------
    "Compute" is a function which will just set the variable COUNT to a perticular value.
    ----------------------------------------------------------------------------
    Code:
    Void Compute(channel)
    {  .....
     switch(channel)
     {
       case 0 :  REG_ADDR.SM0.COUNT = 10;
                 break;
       case 1 :  REG_ADDR.SM1.COUNT = 10;
                 break;  
       default : 
                break ;
      }
     }
    ----------------------------------
    here in the above 'Compute' function if you see, only the
    " SM(X) " is only changing in the case statements .
    But is there a way where i can avoid "Switch" . and do like follwing
    which will save me some lines of code

    Code:
     Compute(channel)
     {
        REG_ADDR.SM[channel].COUNT = 10;  
      }
    Because i dont want to write so many switch cases
    Note: i have taken part of code only in this ..

    Are there any ways which is better in this case ?

    Thanks
    prabhakar

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    You can use an array of structures instead of a bunch of structures. Instead of having:
    Code:
    typedef struct TOTAL_REGS_tag
    { 
        REG_TYPE_T         SM0;
        REG_TYPE_T         SM1;
     }TOTAL_REGS_T;
    replace it with
    Code:
    typedef struct TOTAL_REGS_tag
    { 
        REG_TYPE_T         SM[2];
     }TOTAL_REGS_T;
    That would have an SM[0] and an SM[1]. Is that what you were looking for?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Yes , i can replace with array of structures .
    but ,my issue is something like template in c++ ,where i wanna do like the below
    Code:
    Compute(channel)
     {
        REG_ADDR.SM[channel].COUNT = 10; 
      }
    i want to get rid of this
    SM0 or SM1 and i want to replace with
    SMX in "Compute" Function.
    See First post for "Compute" Function.

    Thanks
    pprabhakar

  4. #4
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    That is how you do it. Make it an array of structures and then that works. Maybe I dont understand the question?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to set pointer for environment variable
    By spiky1 in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2006, 05:19 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Trying to set a max for my variable...
    By GameGenie in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2005, 06:55 PM
  4. what does a structure variable mean?
    By vsriharsha in forum C Programming
    Replies: 9
    Last Post: 08-26-2004, 10:52 PM
  5. Replies: 12
    Last Post: 10-14-2003, 10:17 AM