title image


Smiley Re: Parameter verschiedenen Typs in einer Tabelle abspeichern
Hier ein Beispiel:





#include <stdio.h>



typedef union {

short s16;

int s32;

unsigned short u16;

unsigned int u32;

} union_value;



typedef struct {

char * name;

char * value_type;

union_value value;

} struct_comm;



const struct_comm struct_comm_tab[]={

{"Parameter1", "s16",(short) -16},

{"Parameter2", "s32", (int) 32},

{"Parameter3", "u32", (unsigned int)99},

{"Parameter4", "u16", (unsigned short)88},

};



int main()

{

int i;

for(i=0; i < sizeof(struct_comm_tab)/sizeof(struct_comm);i++)

{

printf("%s %s ",struct_comm_tab[i].name,

struct_comm_tab[i].value_type);

if(strcmp(struct_comm_tab[i].value_type,"s16") == 0)

{

printf("%d\n",struct_comm_tab[i].value.s16);

continue;

}

if(strcmp(struct_comm_tab[i].value_type,"u16") == 0)

{

printf("%u\n",struct_comm_tab[i].value.u16);

continue;

}

if(strcmp(struct_comm_tab[i].value_type,"s32") == 0)

{

printf("%d\n",struct_comm_tab[i].value.s32);

continue;

}

if(strcmp(struct_comm_tab[i].value_type,"u32") == 0)

{

printf("%u\n",struct_comm_tab[i].value.u32);

continue;

}

}

return 0;

}



geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: