简体   繁体   中英

Initializing array in a to struct

how can i initialize an array at once instead of doing it one by one,which is tedious because i need to initialize many arrays with pwm dutycycle values ?

typedef struct
    {
        uint16_t  *pointer;
        uint16_t WELDx_Table[19];
        
    }   WELDx;
    
    WELDx *weldx ;
    
int main(void )
{
    weldx->pointer= &weldx->WELDx_Table[0];
    weldx->pointer[0]=9;
    weldx->pointer[1]=1;    

    ""     ""     ""  ""
    ""     ""     ""  ""
    weldx->pointer[17]=9;
    weldx->pointer[18]='F';
}

Thanks ,

You cannot initialize any memory that is allocated dynamically. You can only initialize variables during their definition. If weld was a struct variable instead of a pointer, you could do it like this:

WELDx weldx =
{
  .WELDx_Table = {9, 1, 8, 2, ..., 9, 'F'},
  .pointer = &weldx.Weldx_Table;
};

This syntax is only available for initialization, but not for assigning values to a variable afterwards.

What you could do is to create a compound literal and copy it into place:

  WELDx *weldx = malloc(sizeof (*weldx));
  memcpy(weldx->WELDx_Table, &(uint16_t[19]){9, 1, 8, 2, ..., 9, 'F'}, sizeof(uint16_t[19]));
  weldx->pointer = weldx->WELDx_Table;

I started to write an answer but soon realized that none of this code makes much sense. So here is a code review instead:

  • The uint16_t *pointer; is completely useless since the array WELDx_Table decays into a pointer with the address you want whenever used in an expression.
  • WELDx *weldx; needs to be initialized somewhere, to point at allocated data. It isn't clear why you declared it as a pointer to begin with.
  • We soon realize that all of this is unnecessary bloat and you can replace the whole thing with a plain array uint16_t WELDx_Table[19]; . That array should be declared const unless you plan to change it in run-time.

Once you have cleaned up the above you can worry about initialization.

Thank you , @Lundin ,@Gerhardh for answering . Firstly I haven't defined actually why im using structs in the first place , i will elaborate , but please mind my c , as im new and definitely trying to learn more .

Weld_Softstart.h

     /*Sofstart variable*/
        extern volatile uint16_t Softstart_1ms_Table[PULSE_NO];
        extern volatile uint16_t *Softstart_1ms_Ptr_Last_elem;
        extern volatile uint16_t *Softstart_1ms_Ptr;
        extern volatile bool Welding_Proc_Init;
        extern volatile u16 uS_cnt ;
        
        /*MainPulse variable*/
        extern volatile uint16_t MainPules_9ms_Table[];             

I have declared these in my Weld_Softstart.c file , I wanted to use structs to make it look more cleaner ,im using volatile beacuse all of these variables are being used in side an Interrupt service routine,also declared extern because im using them across multiple .c files and i need the same instances.Then in a function i am continuously incrementing Softstart_1ms_Ptr until it reaches Softstart_1ms_Ptr_Last_elem which is one element before my terminating character,this is the reason why using a pointer.Lastly im not declaring my array as const because im actually subtracting each element with a value. .Now i was just trying to translate this into a struct

Interruptx_Handler.c

if( (*(Softstart_1ms_Ptr)) == (*(Softstart_1ms_Ptr_Last_elem)) )                // checking for last element , debug point.
        {
        __NOP();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM