Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Pointer To Array (http://www.go4expert.com/forums/pointer-to-array-t9395/)

boschow 17Mar2008 13:14

Pointer To Array
 
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.

asadullah.ansari 17Mar2008 14:34

Re: Pointer To Array
 
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;
}



All times are GMT +5.5. The time now is 16:15.