1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pointer To Array

Discussion in 'C' started by boschow, Mar 17, 2008.

  1. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,

    I am programing in C and i just came across a problem with the pointer to array. I am tring to do a system that has structures like Digital Inputs, Digital Outputs, Analog Inputs exc... All structures have the same charateristics ...

    Code:
    typedef union
    {
        structu
        {
             unsigned bs:1;
             unsigned bc:1;
             unsigned ba:1;
             unsigned ac:1;
        }bit;
        shor int all;
    }DI_Bits;
    
    typedef sruct
    {
       DI_Bits   bits;
       UINT16 alm_timer;
       UINT16 delay_time;
       UINT16 sim_delay;
    }DigitalInputs;
    
    Since this are separate structures I put bits in an array that holds just bits and "register" in an array that holds just registers.

    Code:
    // header file
    static short int *bits[20];
    static short int *reg[20];
    
    Then i made an object to DigitalInputs structure and ...
    Code:
    //source file
    DigitalInputs DI;
    //for bits
    static short int *bits = {
        &DI.bits.all, // this way I included all other structures that holds the bits
    };
    // for registers
    static short int *bits = {
       &DI.alm_timer,
       &DI.delay_time,
       &DI.sim_delay
    };
    
    When I try to compile this with Dev-Cpp, it says that an redelaration occured, while on the Freescale CW just compile this without any problem.
    So i am wantering what I did wrong. Since I want to test this with Dev-Cpp before uploading the software on the microchip I am cindly aking you guys for some help.

    Thanks and
    Best Regards,
    BoSCHoW.
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Code:
    #include<stdio.h>
    #define UINT16 short int //Just define whatever you want 
    typedef union
    {
        struct
        {
             unsigned bs:1;
             unsigned bc:1;
             unsigned ba:1;
             unsigned ac:1;
        }bit;
        short int all; // you cannt put int with short
    }DI_Bits;
    
    typedef struct
    {
       DI_Bits   bits;
       UINT16 alm_timer;//define it
       UINT16 delay_time;
       UINT16 sim_delay;
    }DigitalInputs;
    
    //static short int *bits[20];   // one time
    //static short int *reg[20];
    
    
    //source file
    DigitalInputs DI;
    //for bits
    // second time
    static short int *bits[20] = {    
        &DI.bits.all, // this way I included all other structures that holds the bits
    };
    
    // for registers
    static short int *reg[20] = {
       &DI.alm_timer,
       &DI.delay_time,
       &DI.sim_delay
    };
    
    int main():w!
    
    {
      return 0;
    }
     
    Last edited by a moderator: Mar 18, 2008

Share This Page