Hi anna, On Sat, Jan 29, 2011 at 01:26:46PM +0200, Anna Sidera wrote: > Hello, > > I have a program that uses some input parameters. One of these parameters, for example my_input_parameter, takes values 1, 2, 3 and 4. I want to define an array in which each element is a structure. But I want the structure to depend on the value of my_input_parameter. For example: > > struct my_structure { > <things that depend on the value of my_input_parameter> > }; > struct my_structure *my_array = (struct my_structure *)malloc(10000*sizeof(struct my_structure)); > > Can you tell me any way to do this? Well, the problem is: the decision, what your structure looks like exactly, has to be taken at compile-time -- the compiler has to know it. The input-parameter is only known at runtime -- and then it's to late to change the structure. So if I understand correctly, your goal is impossible as far as I know in compiled languages like C, but would only be possible in interpreted languages like python (there, you can change data-types during runtime). What you can however do: 1) Fix the input-parameter already at compile-time, and compile four different programs (for my_input_parameter=1,2,3 and 4). Then you could write something like this: struct my_structure{ #if my_input_parameter == 1 ... #endif #if my_input_parameter == 2 ... #endif #if my_input_parameter == 3 ... #endif #if my_input_parameter == 4 ... #endif }; and compile the code with e.g. "gcc -Dmy_input_parameter=2" That will give you best runtime performance -- and inÑtead of passing the input parameter, you have to call the appropriate program. Of course, another possibility here would be to link these four cases into a single program -- and the call the appropriate routine depending on your input parameter. 2) you can use C++ and virtual functions (however, this might be bad for the runtime-performance, depending on how you call the code. Calling a virtual function needs more time than "normal" functions (and in general they can't be inlined) ). Defining an abstract base-class, which only contains your interface: class my_structure{ public: virtual double get_element() = 0; } then you can again create 4 independent classes for your 4 parameters: class my_structure_1{ // for my_input_parameter == 1 public: double get_element(){ ....} }; class my_structure_2{ // for my_input_parameter == 2 public: double get_element(){ ....} }; ... 3) you can do something analogous in C by using a structure with function pointers, which you define to behave differently for my_input_parameter == 1,2,3 or 4. Axel